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
- Introduction
- Collection Interface
- List Interface and Implementations
- Set Interface and Implementations
- Queue Interface and Implementations
- Deque Interface and Implementations
- Map Interface and Implementations
- Other Utility Classes
- Hierarchy Diagram
- 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
- ArrayList
- LinkedList
Vector
Stack
(extendsVector
)
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
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 implementsDeque
)PriorityQueue
ArrayDeque
(also implementsDeque
)
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
- HashMap
- LinkedHashMap
- TreeMap
Hashtable
Properties
(extendsHashtable
)
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:- LinkedBlockingQueue
- ArrayBlockingQueue
- PriorityBlockingQueue
- DelayQueue
- SynchronousQueue
- LinkedBlockingDeque
- LinkedTransferQueue
- CopyOnWriteArrayList
- CopyOnWriteArraySet
- ConcurrentSkipListSet
- ConcurrentHashMap
- ConcurrentSkipListMap
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
Post a Comment
Leave Comment