The Java Collections Framework offers a set of interfaces and classes whose purpose is to store multiple objects.
It has two main families, each defined by an interface :
- java.util.Collection – to manage a group of objects;
- java.util.Map – to manage key/value pairs.

What You Need
- About 3 minutes
1.Collection Interface
The Collection interface in Java is the root interface in the Java Collections Framework, which provides a unified API for working with collections of objects.
It defines a basic set of operations for adding, removing, and querying elements in a collection.
The Collection interface defines several methods :
Method | Role |
---|---|
boolean add(E e) (optional) | Add item to collection |
boolean addAll(Collection c) | Add all the elements of the collection provided as a parameter in the collection (optional) |
void clear() | Remove all items from the collection (optional) |
boolean contains(Object o) | Return a boolean that specifies if the element is present in the collection |
boolean containsAll(Collection c) | Return a boolean that specifies whether all the elements provided as parameters are present in the collection |
boolean equals(Object o) | Check for equality with the collection provided as a parameter |
int hashCode() | Return the hash value of the collection |
boolean isEmpty() | Return a boolean indicating if the collection is empty |
Iterator iterator() | Return an Iterator that allows the traversal of the elements of the collection |
boolean remove(Object o) | Remove an item from the collection if present (optional) |
boolean removeAll(Collection c) | Delete all elements provided in collection parameters if present (optional) |
boolean retainAll(Collection c) | Leave in the collection only the elements provided as parameters: the other elements are deleted (optional). It returns a boolean indicating whether the content of the collection has been modified |
int size() | Return the number of elements contained in the collection |
Object[] toArray() | Return an array containing all elements of the collection |
T[] toArray(T[] a) | Return a typed array of all elements in the collection |
Some methods of this interface can raise an UnsupportedOperationException exception because their implementation is optional: add(), addAll(), remove(), removeAll, retainAll() and clear().
There is no direct implementation of Collection interface but it has child interfaces that define the functionalities of different families of collections : List, Queue, Set.
Those interfaces are implemented by several classes in Java, like ArrayList, PriorityQueue, HashSet etc.
Each of these classes provides a different implementation of a collection, with different performance characteristics and memory usage.
1.1 List Interface
A collection of type List is a simple, ordered collection of items that allows duplicates. Since the list is ordered, an element can be accessed from its index.
Implementation | Role |
---|---|
java.util.Vector<E> | A thread-safe implementation provided since Java 1.0, it is like the dynamic array which can grow or shrink its size |
java.util.Stack<E> | An implementation of a stack, it inherits from the Vector class and provides operations of LIFO (Last In First Out) |
java.util.ArrayList<E> | Like Vector but it is an implementation that is not thread-safe |
java.util.LinkedList<E> | An unsynchronized implementation of a doubly linked list, the insertions of new elements are very fast |
java.util.concurrent.CopyOnWriteArrayList<E> | A thread-safe variant of the ArrayList class in which all operations modifying the contents of the list recreate a new copy of the array used to store the elements of the collection |
1.2 Set Interface
A collection of type Set contains no duplicate elements and it is not possible to direct access to an element. It has two child interfaces : SortedSet and NavigableSet.
Implementation | Role |
---|---|
java.util.HashSet<E> | It is not a thread-safe implementation but it is possible to add a null element |
java.util.LinkedHashSet<E> | Similar to HashSet by defining the traversal order which is the one in which the elements were added |
java.util.TreeSet<E> | The elements are sorted but it is not thread safe and cannot add null element |
java.util.concurrent.ConcurrentSkipListSet<E> | An ordered set of elements capable of handling strong concurrency |
java.util.concurrent.CopyOnWriteArraySet<E> | A thread-safe implementation by creating a new copy when invoking a method that modifies the contents of the collection |
java.util.EnumSet<E extends Enum<E>> | All elements of the collection must belong to the same enumeration |
1.3 Queue Interface
A Queue is a collection that stores items in a certain order before being consumed for processing.
Most of the implementations proposed by the Collection Framework use the FIFO order (First In, First Out).
The Queue interface has some subinterfaces like : BlockingQueue, Deque, TransferQueue, BlockingDeque.
Implementation | Role |
---|---|
java.util.concurrent.LinkedTransferQueue<E> | Its ability is to transfer elements between threads in a way that blocks the producer until the element is consumed by a consumer. This transfer is done using the transfer() method, which will block the calling thread until the element has been consumed by a consumer thread. |
java.util.concurrent.ArrayBlockingQueue<E> | Its capacity is specified at the time of its creation and cannot be changed thereafter. When an element is added to a full ArrayBlockingQueue, the adding thread is blocked until a space becomes available. Similarly, when an element is removed from an empty ArrayBlockingQueue, the removing thread is blocked until an element becomes available. |
java.util.concurrent.DelayQueue<E extends Delayed> | It is a blocking queue that could be used in producer-consumer programs. It has a very useful characteristic – when the consumer wants to take an element from the queue, they can take it only when the delay for that particular element has expired. |
java.util.concurrent.LinkedBlockingQueue<E> | It is an optionally-bounded blocking queue implementation, meaning that the queue size can be specified if needed. The capacity, if unspecified, is equal to Integer.MAX_VALUE. |
java.util.concurrent.SynchronousQueue<E> | It is a blocking queue in which each insert operation must wait for a corresponding remove operation by another thread and vice versa. |
java.util.concurrent.PriorityBlockingQueue<E> | It is a unbounded queue which allows to block when removing from an empty queue. The elements in the PriorityBlockingQueue are ordered by priority. The highest priority element is always ordered first. When two elements are compared and are the same, there’s no guarantee of how they will be ordered. |
java.util.PriorityQueue<E> | The elements in PriorityQueue are ordered according to their natural ordering or a custom comparator. We can not just add any type of element to a PriorityQueue, either adding elements which implement Comparable or adding elements which do not implement Comparable but providing a Comparator as well. |
java.util.ArrayDeque<E> | Array Double Ended Queue is a special kind of a growable array that allows to add or remove an element from both sides. It can be used as a Stack (Last-In-First-Out) or a Queue (First-In-First-Out). |
java.util.concurrent.LinkedBlockingDeque<E> | It is a thread-safe and concurrent implementation of the Deque interface that uses a linked list to store its elements. It is optionally-bounded. When unspecified, the capacity is by default taken as Integer.MAX_VALUE. It is a BlockingDeque, which means that threads that try to insert elements into a full deque or remove elements from an empty deque will be blocked until space is available or elements are available. |
2.Map Interface
The Map interface in Java is part of the Java Collections Framework and provides a way to store key-value pairs.
Each key in a Map is associated with a single value, and the keys in a Map must be unique.
On the other hand, the same value can be associated with several different keys.
The Map interface defines several methods :
Method | Role |
---|---|
void clear() | Remove all items from the collection |
boolean containsKey(Object) | Indicate if the key is contained in the collection |
boolean containsValue(Object) | Indicate if the value is contained in the collection |
Set entrySet() | Return a set containing the collection’s key/value pairs |
Object get(Object) | Return the value associated with the key provided as a parameter |
boolean isEmpty() | Return a boolean indicating if the collection is empty |
Set keySet() | Return a set containing the collection keys |
Object put(Object, Object) | Insert the key and its associated value provided as parameters |
void putAll(Map) | Insert all the keys/values of the object provided as a parameter |
Collection values() | Return a collection that contains all element values |
Object remove(Object) | Delete the element whose key is provided as a parameter |
int size() | Return the number of elements contained in the collection |
The Map interface has several child interfaces like SortedMap, ConcurrentMap etc.
The implementations of those interfaces include : HashMap, LinkedHashMap, TreeMap etc.
Implementation | Role |
---|---|
java.util.Hashtable<K,V> | It maps keys to values and is thread safe. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. It can be traversed by Enumerator and Iterator. |
java.util.HashMap<K,V> | Like Hashtable but it is not thread safe. It allows one null key and multiple null values. It can not be traversed by Enumerator. |
java.util.TreeMap<K,V> | It keeps the entries sorted according to the natural ordering of keys or better using a comparator if provided at construction time. |
java.util.EnumMap<K,V> | It exclusively takes Enum as its keys. It does not permits null key, but permits multiple null values. It is not thread-safe. Its performance is better than HashMap. |
java.util.IdentityHashMap<K,V> | The Map interface mandates the use of the equals() method on the key comparison. However, the IdentityHashMap class violates that contract. Instead, it uses reference equality (==) on key search operations. |
java.util.WeakHashMap<K,V> | It is a hashtable-based implementation of the Map interface, with keys that are of a WeakReference type. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use, meaning that there is no single Reference that point to that key. When the garbage collection (GC) process discards a key, its entry is effectively removed from the map. |
java.util.LinkedHashMap<K,V> | The LinkedHashMap class is very similar to HashMap in most aspects. However, it is able to maintain the order of elements either by Insertion-Order or by Access-Order. |
java.util.concurrent.ConcurrentHashMap<K,V> | HashMap is not a thread-safe implementation of Map interface. Even if Hashtable is thread safe, it is not very efficient. ConcurrentHashMap is thread-safe and efficient by locking only the portion of the map being modified, rather than the entire map. |
java.util.concurrent.ConcurrentSkipListMap<K,V> | All the elements of ConcurrentSkipListMap are sorted based on natural ordering or by the Comparator passed during it’s construction time. This class uses a concurrent variation of SkipList data structure providing log(n) time cost for insertion, removal, update, and access operations. These operations are safe for executing concurrently by multiple threads. |
The Map interface defines an internal Map.Entry interface that defines functionality for an object that encapsulates a key/value pair.
It is recommended to use immutable objects as keys.
A Map can be traversed in three ways:
- traversing all the keys;
- traversing all the values;
- traversing a set of key/value pairs.