Introduction to Java Collection Framework

A Java Collection is a group of similar type of objects. Each object in a collection is called as an element. The Java Collection Framework is a set of various interfaces, implementations and algorithms used to represent and manipulate a collection.

The Collection Hierarchy

The root of the Java collection hierarchy is the Collection interface. Java doesn’t provide any direct implementation of this interface. It provides implementation of more specific sub-interfaces like Set and List.

A Collection implementation that impalements any of the sub-interfaces of Collection interface should provide two constructors, a no-argument constructor which creates an empty collection, and a constructor with a single argument of type Collection to create a new collection with same elements as passed in the argument. Though it’s not a mandate, these are general guidelines followed by all implementation of Collection provided by Java platform library.

Any method that modifies the collection on which they operate should throw UnsupportedOperationException if the collection does not support the operation.

The Java Collection hierarchy with the most commonly used classes is shown below:

Java Collection Hierarchy

The Collection interface is the root of the hierarchy, even though it extends the Iterable interface. Iterable is not part of the Collection framework, it is present in the java.lang package and allows iteration over the Collection. The Collection framework is a part of java.util package.

We can notice from the collection hierarchy that we do not have the Map interface in the hierarchy. That is because Map interface does not extends Collection, even though it is a part of the Collection Framework

So if Map is a part of Collection Framework why doesn’t it extend Collection interface?

That’s because Collection interface is incompatible with Map interface. A collection is a group of object but a Map is a group of key-value pairs. The two interfaces have different semantics and most methods declared in the Collection interface would not fit a Map and vice versa. For example, Collection uses the add(Object) method to add a new element to the collection, while Map uses a put(key, value) method to do the same.

The AbstractCollection class

As the name suggest the AbstractCollection class is an abstract class that implements the Collection Interface. It provides a skeletal implementation of the Collection interface to reduce the efforts required to implement it. It provides implementation of some general purpose collection methods like isEmpty(), contains(Object), toArray(), clear(), addAll(Collection), removeAll(Collection), toString() etc.


Internal Implementation of ArrayList

ArrayList is an implementation of List interface, used to store a list of objects. Unlike normal arrays, it is a dynamic data structure, its size dynamically increases when its capacity is exhausted.

View Article

Understanding equals() and hashCode() methods

The equals() and hashCode() are two primary methods of Java's Object Class which work together to uniquely identify any object in Java.

View Article

Internal Implementation of HashMap

HashMap is popular data structure in Java which stores data in key-value format. This article describes the internal implementation to store and retrieve data in a hashmap.

View Article

Internal Implementation of HashSet

HashSet is a member of the Java Collection Framework which is used to store unique entries. This article describes how a HashSet is internally implemented to store unique entries.

View Article

Identity HashMap in Java

IdentityHashMap in Java is a special implentation of the Map interface designed to be used in special cases where reference equality is required.

View Article

Iterator and ListIterator in Java

The Java API provides the Iterator and ListIterator that are used to iterate over a Collection. This article covers the difference between the two and discuss how Emumeration is different from Iterat.

View Article

Understanding TreeMap

A TreeMap is an implementation of the Map interface used to store key-value pair in sorted order of their keys. This articles describe how a Treemap is implemented in Java.

View Article