Java Collections Framework : High Level Overview

In this article, we will look into the high level view of Java Collections Framework.

The Java Collections Framework is a powerful and extensive library built into the Java programming language. It provides a set of interfaces, classes, and algorithms to efficiently store, manipulate, and process collections of objects.

Collection Interface and Sub Interfaces

Java Collections Framework: Java Collections

In the above figure, we have the Java Collections hierarchy , starting with the Collection interface.

InterfaceDescriptionConcrete Classes
CollectionPrescribes the basic operations for a collection of objects. 
SetExtends Collection interface. Represents a set of unique elements.HashSet, LinkedHashSet
SortedSetExtends Set interface. Stores unique elements in sorted order.TreeSet
ListExtends the Collection interface. Maintains a sequence of elements.ArrayList, Vector, LinkedList
QueueExtends the Collection interface. Represents a collection designed for holding elements prior to processing. Supports operations like insertion, removal, and retrieval.LinkedList, PriorityQueue

Concrete Classes

Concrete ClassInterfaceIs Duplicate Allowed?Ordered/SortedData Structure
ArrayListListYesInsertion OrderResizable array
LinkedListListYesInsertion OrderLinked list
VectorListYesInsertion OrderResizable array
HashSetSetNoNo OrderHash table
LinkedHashSetSetNoInsertion OrderHash table and doubly-linked list
TreeSetSortedSetNoSortedBalanced Tree
LinkedListQueueYesInsertion OrderLinked list
PriorityQueueQueueNoNo OrderHeap

Methods of Collection interface

Some commonly used methods in the Java Collection interface are mentioned below:

MethodDescription
boolean add(E element)Adds the specified element to the collection.
boolean contains(Object o)Returns true if the collection contains the specified element.
boolean remove(Object o)Removes the specified element from the collection.
int size()Returns the number of elements in the collection.
boolean isEmpty()Returns true if the collection is empty.
void clear()Removes all elements from the collection.
Iterator<E> iterator()Returns an iterator over the elements in the collection.
boolean addAll(Collection<? extends E> c)Adds all elements from the specified collection to the collection.
boolean removeAll(Collection<?> c)Removes all elements in the specified collection from the collection.
boolean retainAll(Collection<?> c)Retains only the elements in the collection that are contained in the specified collection.
boolean containsAll(Collection<?> c)Returns true if the collection contains all elements in the specified collection.
Object[] toArray()Returns an array containing all elements in the collection.
boolean equals(Object o)Compares the specified object with the collection for equality.
int hashCode()Returns the hash code value for the collection.

Queue Hierarchy

Let us have a look into the Queue hierarchy.

Java Collections Framework: Queue Hierarchy
InterfaceDescriptionConcrete Classes
QueueIt extends the Collection interface. Designed to have elements inserted at the end and removed from the beginning.ArrayQueue, PriorityQueue
DequeExtends the Queue interface. Supports adding and removing elements from both ends.ArrayDeque, LinkedList
BlockingQueueExtends the Queue interface. Supports blocking operations for waiting on elements and space availability.ArrayBlockingQueue, PriorityBlockingQueue, LinkedBlockingQueue
BlockingDequeExtends both the Deque and BlockingQueue interfaces. Supports blocking operations for adding and removing elements.LinkedBlockingDeque

Let us look into some of the important implementations for Queue.

Concrete ClassInterfaceData Structure
ArrayQueueQueueArray
PriorityQueueQueueArray
ArrayBlockingQueueBlockingQueueArray
PriorityBlockingQueueBlockingQueueArray
LinkedBlockingQueueBlockingQueueLinked list
ArrayDequeDequeArray
LinkedListDequeLinked list
LinkedBlockingDequeBlockingDequeDoubly linked list

Map Interface

The Map interface is a key component of the Java Collections Framework. It represents a collection of key-value pairs, where each key is unique and maps to a corresponding value. The Map interface provides methods for adding, retrieving, and manipulating key-value pairs, making it a versatile data structure for various applications.

Please note that Map interface does not extend the Collection interface as Map is not a collection. It contains entries of key-value pairs.

Let us have a look into the Map hierarchy.

Java Collections Framework:Map Hierarchy
InterfaceDescriptionConcrete Classes
MapDefines operations for maintaining key-value mappings.HashMap, Hashtable, LinkedHashSet
SortedMapExtends the Map interface. Mappings are sorted by key order.TreeMap
Concrete ClassInterfaceIs Duplicate Key Allowed?Ordered/SortedData Structure
HashMapMapNoNo OrderHash table
LinkedHashMapMapNoKey insertion orderHash table and doubly-linked list
HashtableMapNoNo OrderHash table
TreeMapSortedMapNoSorted in Key orderBalanced Tree

Java Iterator Interface

The Java Iterator interface provides a way to iterate over elements in a collection sequentially. It allows you to traverse and access elements in a collection without exposing the underlying implementation details. The Iterator interface is part of the Java Collections Framework and is widely used across various collection classes.

Here is an overview of some commonly used methods in the Iterator interface:

MethodDescription
boolean hasNext()Returns true if there are more elements in the collection, otherwise returns false.
E next()Returns the next element in the collection.
void remove()Removes the last element returned by next() from the underlying collection (optional operation).
default void forEachRemaining(Consumer<? super E> action)Performs the specified action on each remaining element in the collection until all elements have been processed or an exception is encountered.

To use an Iterator, you typically obtain an instance of it by calling the iterator() method on a collection object. Here’s an example that demonstrates the usage of Iterator:

List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");

Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
    String name = iterator.next();
    System.out.println(name);
    if (name.equals("Bob")) {
        iterator.remove();
    }
}

In this example, we create an ArrayList called names and add three names to it. We obtain an iterator using the iterator() method and use it to traverse the collection. The hasNext() method checks if there are more elements, and the next() method returns the next element in the collection. We can perform operations on each element, and if needed, remove elements using the remove() method.

The Iterator interface provides a standardized way to iterate over collections, making it easier to work with various types of collections in a consistent manner.

Conclusion: Java Collections Framework

In summary, the Collections in Java encompass a robust and all-encompassing framework consisting of interfaces, classes, and algorithms designed for efficient handling of object collections in the Java programming language. It provides developers with a comprehensive view of diverse data structures and algorithms, empowering them to effectively store, manipulate, and process collections of elements.

Throughout this article, we have delved into the fundamental components of the Java Collections Framework, including interfaces like Collection, List, Set, Queue, and Map, as well as their respective implementations.

By harnessing the power of the Java Collections Framework, developers can unlock the benefits of optimized performance, increased productivity, and elevated code quality, ultimately leading to more effective software development and delivery.

Leave a Reply

Your email address will not be published. Required fields are marked *