The Java Collection Framework

  • The Collection interfaces
    • Duplicates
    • Sortability
  • The Collection interface
    • The Set interface
    • The List Interface
    • The Queue Interface
    • The SortedSet Interface
    • The Map Interface
    • The SortedMap Interface

The Collection interfaces

Collections a container for objects, Maps are container for key-value pairs. Three criteria specify the sort of container: - Sortability - Uniqueness / Duplicates - Order of elements

Collections overview


Duplicates are defines as objects returning true on the equals(E e )-method call. This can vary per implementation.


Sortable elements need to implement the compareTo()-method or use a data type that has this method implemented ( the natural order of the type).

The Collection interface

The basic interface contains following methods:

public boolean add(e);
public boolean addAll(c);
public void clear();
public boolean contains(o); 
public boolean containsAll(c);
public boolean equals(Object o);
public int hashCode();
public boolean isEmpty();
public Iterator<E> iterator()
public boolean remove(o);
public boolean removeAll(c)
public boolean retainAll(c)
public int size();
public Object[] toArray()
public <T> T[] toArray(T[] a)

The Set interface

Has all methods of Collection but different implementation of some. No duplicates are allowed. Can be used for mathematic operations as e.g. join two sets, combine two sets or compare two sets.

The List Interface

Represents data with a defined order. On top of methods of Collection, it has a lot of methods to access elements index-wise. But also the iterator allows to loop over all elements position-wise, making this interface a sequence-like ADT.

The Queue Interface

Has methods allowing implementations of the stack and queue ADT.

The SortedSet Interface

Elements of the SortedSet are sortable. If no parameter is specified the natural order (order of the datatype) is used. It is also possible to define your own Comparator class which implements the Comparator interface.

Class ordering String-objects first on length, then on alphabetical value.
static class LengthAlphaComparator implements Comparator<String> {
public int compare(String s1, String s2) {
if (s1.length() < s2.length()){
return -1;
if (s1.length() > s2.length()){
return 1;

return s1.compareTo(s2);

SortedSet<String> mySet = new TreeSet<>(new LengthAlphaComparitor());

The Map Interface

Most methods of this interface give access to objects by its key value.

The SortedMap Interface

In a SortedMap are the keys sortable. The keys are a SortedSet and can be iterated in the order specified by the Comparator class.

Overview of classes implementing different Collection interfaces
The huge advantage of these classes is that they allow to organize data in a certain way. The set for example does not allow duplicates, so you do not need to do the check manually. The Sorted Map already sorts your data following a defined criterion and new data is added in the same way, so ordering at a later stage is not necessary.