The notes I took on Java's Collection Framework


[60]
Java Collection Framework
The Java Collection Framework is a set of classes and interfaces in Java that provide commonly reusable, type-safe and efficient data structures to store and manipulate group of objects. The framework consists of several key interfaces and classes grouped into two main hierarchies: the Collection hierarchy and the Map hierarchy.
Collection hierarchy
The root interface for most of the collection types. It defines the basic methods common to all collections, such as add()
, remove()
, contains()
and size()
.
List interface
It extends Collection
and represents an ordered collection that allows duplicate elements. Implementations include ArrayList
, LinkedList
and Vector
.
Set interface
It also extends Collection
and represents a collection that does not allow duplicate elements. Implementations include HashSet
, TreeSet
and LinkedHashSet
.
Queue interface
It extends Collection
and represents a collection designed for holding elements before processing. Implementations include LinkedList
and PriorityQueue
.
Map hierarchy
It represents a collection of key-value pairs. Each key must be unique, and it maps to a specific value. Common implementations include HashMap
, TreeMap
and LinkedHashMap
.
Concurrency Collections
The framework also includes thread-safe implementations of various collection interfaces, such as ConcurrentHashMap
, CopyOnWriteArrayList
and CopyOnWriteArraySet
, which are designed to be used in concurrent programming scenarios.
Legacy Classes
Some older classes, such as Vector
and Hashtable
, are part of the framework but are considered legacy. They have been largely superseded by more modern implementations like ArrayList
and HashMap
.
Collection interface
The Collection
interface is a root interface in the Java Collection Framework. It is a part of the java.util
package and provides a way to represent and manipulate groups of objects. The Collection
interface extends the Iterable
interface, making it suitable for use in enhanced for loops.
public interface Collection<E> extends Iterable<E> {
// Basic operations
// Adds the specified element to the collection
boolean add(E element);
// Adds all elements from a specified collection to this collection
boolean addAll(Collection<? extends E> collection);
// Removes all elements from the collection
void clear();
// Checks if the collection contains the specified element
boolean contains(Object element);
// Checks if the collection conatins all elements from a specified collection
boolean containsAll(Collection<?> collection);
// Checks if the collection is empty
boolean isEmpty();
// Removes the specified element from the collection
boolean remove(Object elements);
// Removes all elements that are present in a specified collection
boolean removeAll(Collection<?> collection);
// Retains only the elements that are present in a specified collection
boolean retainAll(Collection<?> collection);
// Returns the number of elements in the collection
int size();
// Converts the collection to an array
Object[] toArray();
// Converts the collection to an array of a specified type
<T> T[] toArray(T[] array);
}
This Collection
interface is generic, meaning it can be parameterized with the type of elements it will contain (E
in the above definition).
Subscribe to my newsletter
Read articles from Pranav Bawgikar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Pranav Bawgikar
Pranav Bawgikar
Hiya ๐ I'm Pranav. I'm a recent computer science grad who loves punching keys, napping while coding and lifting weights. This space is a collection of my journey of active learning from blogs, books and papers.