This section describes the Java Collections Framework. Here, you will learn what collections are and how they can make your job easier and programs better. You'll learn about the core elements interfaces, implementations, aggregate operations, and algorithms that comprise the Java Collections Framework.
Interfaces describes the core collection interfaces, which are the heart and soulof the Java Collections Framework. You'll learn general guidelines foreffective use of these interfaces, including when to use which interface.You'll also learn idioms for each interface that will help you get the mostout of the interfaces.
Implementations describes the JDK's general-purpose collection implementationsand tells you when to use which implementation. You'll also learn aboutthe wrapper implementations, which add functionality to general-purposeimplementations.
Custom Implementations tells you why you might want to write your own collection implementation(instead of using one of the general-purpose implementations provided by theJDK), and how you'd go about it. It's easy with the JDK's abstractcollection implementations!
Interoperabilitytells you how the Collections Framework interoperates with older APIs thatpredate the addition of Collections to Java. Also, it tells you how to designnew APIs so that they'll interoperate seamlessly with other new APIs.
Java Collection means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).
The Iterable interface is the root interface for all the collection classes. The Collectioninterface extends the Iterable interface and therefore all the subclasses of Collectioninterface also implement the Iterable interface.
The Collection interface is the interface which is implemented by all the classes in thecollection framework. It declares the methods that every collection will have. In otherwords, we can say that the Collection interface builds the foundation on which thecollection framework depends.
The ArrayList class implements the List interface. It uses a dynamic array to store theduplicate element of different data types. The ArrayList class maintains the insertion orderand is non-synchronized. The elements stored in the ArrayList class can be randomlyaccessed. Consider the following example.
LinkedList implements the Collection interface. It uses a doubly linked list internally tostore the elements. It can store the duplicate elements. It maintains the insertion order andis not synchronized. In LinkedList, the manipulation is fast because no shifting is required.
The stack is the subclass of Vector. It implements the last-in-first-out data structure, i.e.,Stack. The stack contains all of the methods of Vector class and also provides its methods like boolean push(), boolean peek(), boolean push(object o), which defines itsproperties.
Queue interface maintains the first-in-first-out order. It can be defined as an ordered listthat is used to hold the elements which are about to be processed. There are variousclasses like PriorityQueue, Deque, and ArrayDeque which implements the Queueinterface.
The PriorityQueue class implements the Queue interface. It holds the elements or objectswhich are to be processed by their priorities. PriorityQueue doesn't allow null values to bestored in the queue.
Deque interface extends the Queue interface. In Deque, we can remove and add theelements from both the side. Deque stands for a double-ended queue which enables us toperform the operations at both the ends.
Set Interface in Java is present in java.util package. It extends the Collection interface. Itrepresents the unordered set of elements which doesn't allow us to store the duplicateitems. We can store at most one null value in Set. Set is implemented by HashSet,LinkedHashSet, and TreeSet.
LinkedHashSet class represents the LinkedList implementation of Set Interface. It extendsthe HashSet class and implements Set interface. Like HashSet, It also contains uniqueelements. It maintains the insertion order and permits null elements.
SortedSet is the alternate of Set interface that provides a total ordering on its elements.The elements of the SortedSet are arranged in the increasing (ascending) order. TheSortedSet provides the additional methods that inhibit the natural ordering of the elements.
Java TreeSet class implements the Set interface that uses a tree for storage. LikeHashSet, TreeSet also contains unique elements. However, the access and retrievaltime of TreeSet is quite fast. The elements in TreeSet stored in ascending order.
Data Collection is the next generation of website tag and mobile SDK management capabilities from Adobe. Data Collection gives customers a simple way to deploy and manage the analytics, marketing, and advertising solutions necessary to power relevant customer experiences. Use these videos and tutorials to help learn this critical technology.
Java Collections Framework is one of the core parts of the Java programming language. Collections are used in almost every programming language. Most of the programming languages support various type of collections such as List, Set, Queue, Stack, etc.
Java 1.2 provided Collections Framework that is the architecture to represent and manipulate Collections in java in a standard way. Java Collections Framework consists of the following parts:
java.util.Collection is the root interface of Collections Framework. It is on the top of the Collections framework hierarchy. It contains some important methods such as size(), iterator(), add(), remove(), clear() that every Collection class must implement.
Some important collection classes are ArrayList, LinkedList, HashMap, TreeMap, HashSet, and TreeSet. These classes solve most of our programming needs but if we need some special collection class, we can extend them to create our custom collection class.
Java 1.5 came up with thread-safe collection classes that allowed us to modify Collections while iterating over them. Some of them are CopyOnWriteArrayList, ConcurrentHashMap, CopyOnWriteArraySet. These classes are in java.util.concurrent package.
Java collection interfaces are the foundation of the Java Collections Framework. Note that all the core collection interfaces are generic; for example public interface Collection. The syntax is for Generics and when we declare Collection, we should use it to specify the type of Object it can contain. It helps in reducing run-time errors by type-checking the Objects at compile-time.
The interface has methods to tell you how many elements are in the collection (size, isEmpty), to check whether a given object is in the collection (contains), to add and remove an element from the collection (add, remove), and to provide an iterator over the collection (iterator).
Iterator interface provides methods to iterate over the elements of the Collection. We can get the instance of iterator using iterator() method. Iterator takes the place of Enumeration in the Java Collections Framework. Iterators allow the caller to remove elements from the underlying collection during the iteration. Iterators in collection classes implement Iterator Design Pattern.
List is an ordered collection and can contain duplicate elements. You can access any element from its index. List is more like array with dynamic length. List is one of the most used Collection type. ArrayList and LinkedList are implementation classes of List interface.
Java ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().
SortedSet is a Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls.
A map that maintains its mappings in ascending key order. This is the Map analog of SortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories.
Java Collections framework comes with many implementation classes for the interfaces. Most common implementations are ArrayList, HashMap and HashSet. Java 1.5 included Concurrent implementations; for example ConcurrentHashMap and CopyOnWriteArrayList. Usually Collection classes are not thread-safe and their iterator is fail-fast. In this section, we will learn about commonly used collection classes.
This class offers constant time performance for basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets. We can set the initial capacity and load factor for this collection. The load factor is a measure of how full the hash map is allowed to get before its capacity is automatically increased.
Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal.
Java ArrayList is the resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)
The size, isEmpty, get, set, iterator, and list iterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.
c80f0f1006