An Introduction to the Collection Framework in Java

Hasini Sandunika Silva
3 min readJun 8, 2021
An Introduction to the Collection Framework in Java

Overview

As per the official documentation of the java collection framework, the collection framework is an architecture that provides facilities to store and manipulate a group of objects. This allows the user to perform the basic functionalities on data such as searching, sorting, deletion, insertion, etc. Here, all the classes, interfaces that are defined for the collection framework are inside java.util package. The following figure 1 illustrates the hierarchy of the collection framework in java.

Figure 1. Hierarchy of the Collection Framework in Java.

The java collection framework is a composition of interfaces, algorithms, and classes. Here, all the interfaces are inherited from the collection interface except the Map and SortedMap interfaces. The following defines some of the main interfaces defined in the collection framework.

  • Collection: This represents the top of the hierarchy except the Iterable interface. This includes some common methods such as size(), toArray(), add(), remove(), etc.
  • List: This extends the Collection interface. Using the list interface, we can store a collection of objects. Here the duplicates are allowed.
  • Queue: This follows the first in first out rule which implies this feature is applied to the ordered collection of objects.
  • Set: Sets are used to store elements without any order. But, this does not allow the user to store duplicate elements.
  • Deque: Deque extends the Queue interface. This stands for the double-ended queue which allows the user to add or remove elements at both ends.
  • SortedSet: This extends the Set interface and the elements inside this are arranged in ascending order.
  • Map: This is used to store elements with unique keys.
  • SortedMap: This extends the Map interface and maintains ordered maps.

When we talk about the classes, the following should be mentioned.

  • ArrayList: This class inherits AbstractList class while implementing the List, Clonable, Serializable, and RandomAccess interfaces. When we create an instance of the ArrayList class, it creates a dynamic array.
  • LinkedList: This extends AbstractSequentialList class while implementing List, Clonable, Serializable, and Deque interfaces to create a linked list.
  • Vector: This is also used to implement dynamic arrays but there are some differences between the ArrayList and Vector.
  • Stack: This extends the Vector class while providing additional functionalities related to stacks (last in first out).
  • PriorityQueue: This extends AbstractQueue while implementing the Serializable interface. The elements inside this are processed according to their priorities.
  • ArrayDeque: This extends the AbstractCollection class while implementing Deque, Clonable, and Serializable interfaces. Here we can add or delete elements from both ends.
  • HashSet: HashSet extends AbstractSet while implementing Set, Clonable, and Serializable interfaces. Here, the collections are stored in hash tables. But, here only the unique values are stored.
  • LinkedHashSet: This extends HashSet while implementing Set, Clonable, and Serializable interfaces. As HashSet, here also only the unique values are stored. This maintains the insertion order.
  • TreeSet: This extends AbstractSet class while implementing NavigableSet, Clonable, and Serializable interfaces. As HashSet and LinkedHashSet, this also only allows storing unique elements. The time taken to access the elements in TreeMap is faster than HashSet and LinkedHashSet because the elements are stored in a tree structure while following the ascending order.
  • HashTable: This extends the Dictionary class while implementing Map, Clonable, and Serializable interfaces. Here maps the keys to values and only contains unique elements. HashTable class is synchronized. Here, does not allow null for both key and value.
  • LinkedHashMap: This inherits the HashMap class while implementing the Map interface. Here only contains unique elements. This is non-synchronized. Here map the keys to values and these may hold a single null key and multiple null values.
  • HashMap: this extends AbstractMap class while implementing Map, Clonable, and Serializable interfaces. This only contains unique keys and can have a single null key and multiple null values. This is non-synchronized.
  • TreeMap: This extends AbstractMap class while implementing NavigableMap, Clonable, and Serializable interfaces. TreeMap behaves as a red-black tree and this can have unique elements. Here the key cannot be null but can have null for the value. TreeMaps maintain the ascending order and these are non-synchronized.

References

--

--