Java Collection Framework Hierarchy

The Java Collections Framework provides a set of classes and interfaces for working with collections of objects. It is a part of the java.util package and includes several important interfaces and classes. Below is a comprehensive overview of the Java Collection Framework hierarchy, covering all the primary interfaces and classes.

Table of Contents

  1. Introduction
  2. Collection Interface
  3. List Interface and Implementations
  4. Set Interface and Implementations
  5. Queue Interface and Implementations
  6. Deque Interface and Implementations
  7. Map Interface and Implementations
  8. Other Utility Classes
  9. Hierarchy Diagram
  10. Conclusion

Introduction

The Java Collections Framework is a unified architecture for representing and manipulating collections. It includes interfaces and their concrete implementations to manage collections of objects in various ways.

For an in-depth look at the Java Collections Framework, visit the Java Collections Framework in Depth with Examples for Beginners.

1. Collection Interface

The Collection interface is the root of the collection hierarchy. It provides general methods for adding, removing, and querying elements. Subinterfaces of Collection include List, Set, and Queue.

Collection Interface

public interface Collection<E> extends Iterable<E> {
    boolean add(E e);
    boolean remove(Object o);
    boolean contains(Object o);
    int size();
    boolean isEmpty();
    void clear();
    Iterator<E> iterator();
    // Other methods...
}

2. List Interface and Implementations

The List interface represents an ordered collection (also known as a sequence). Lists can contain duplicate elements.

List Interface

public interface List<E> extends Collection<E> {
    void add(int index, E element);
    E get(int index);
    E remove(int index);
    int indexOf(Object o);
    // Other methods...
}

List Implementations

Special-Purpose List Implementations

3. Set Interface and Implementations

The Set interface represents a collection that does not allow duplicate elements.

Set Interface

public interface Set<E> extends Collection<E> {
    // Methods inherited from Collection
}

Set Implementations

Special-Purpose Set Implementations:

4. Queue Interface and Implementations

The Queue interface represents a collection designed for holding elements prior to processing. Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner.

Queue Interface

public interface Queue<E> extends Collection<E> {
    boolean add(E e);
    E remove();
    E poll();
    E element();
    E peek();
    // Other methods...
}

Queue Implementations

  • LinkedList (also implements Deque)
  • PriorityQueue
  • ArrayDeque (also implements Deque)

5. Deque Interface and Implementations

The Deque interface represents a double-ended queue that supports element insertion and removal at both ends.

Deque Interface

public interface Deque<E> extends Queue<E> {
    void addFirst(E e);
    void addLast(E e);
    E removeFirst();
    E removeLast();
    E getFirst();
    E getLast();
    // Other methods...
}

Deque Implementations

  • LinkedList
  • ArrayDeque

6. Map Interface and Implementations

The Map interface represents a collection of key-value pairs. It does not extend the Collection interface.

Map Interface

public interface Map<K, V> {
    V put(K key, V value);
    V get(Object key);
    V remove(Object key);
    boolean containsKey(Object key);
    boolean containsValue(Object value);
    int size();
    boolean isEmpty();
    void clear();
    Set<K> keySet();
    Collection<V> values();
    Set<Map.Entry<K, V>> entrySet();
    // Other methods...
}

Map Implementations

Special-Purpose Map Implementations

7. Other Utility Classes

Java also provides several utility classes for working with collections.

  • Collections: Contains static methods for manipulating collections (such as sorting and searching).
  • Arrays: Contains static methods for working with arrays (such as sorting and searching).

8. Hierarchy Diagram

Below is a simple textual representation of the Java Collections Framework hierarchy:

The collection interfaces are divided into two groups. The most basic interface, java.util.Collection interface has the following descendants and it's implementations:

 The other collection interfaces are based on java.util.Map and it's implementations:

Concurrent Collections

These concurrent-aware interfaces are available:
The following concurrent-aware implementation classes are available. See the API documentation for the correct usage of these implementations.

Conclusion

The Java Collections Framework provides a unified architecture for managing collections of objects. Understanding the hierarchy and the different interfaces and classes available allows you to choose the appropriate collection for your specific needs. This guide provided an overview of the main interfaces and classes in the framework, along with examples of how to use them.

Comments