Collection Framework in Java

Samyak Shreyash
3 min readJun 1, 2021

To Discuss about collections in a single post would be to write a chapter on topic which ought to be a book. So, I would break it into multiple sections

Image Credit: Data Flair

In general terms, a collection represents a group of objects. In Java, collections are handled by collection framework. Collections framework is an unified architecture for representing and manipulating collections.

Need for Collection Framework

Before JDK 1.2, Java implemented collections with arrays, vectors and hashtables. The implementation of all these collections were defined independently with no correlation among themselves as they had no common interface. So, the user had to remember different methods, syntax and constructors.

With collection framework, Java mandated common implementations, constructors, destructor methods and error codes.

Advantages

1. Reduces Programming effort by providing pre-defined Data Structures and algorithms.
2. Increases Performance by providing high-performance data structures.
3. Provides interoperability between unrelated APIs by establishing common language to pass collections back and forth.
4. Fosters Software Reuse by providing a standard interface for collections and algorithms.

Hierarchy of Collection Framework

The Collection framework extends the iterable interface at its root which is present in the Java.util package.

Iterable interface

It’s the root interface for all collection classes. It contains only one abstract method.

Iterator<T> iterator() 

Collection Interface

Collection interface is implemented by all classes in the collection framework. It declares the methods that every collection will have, thus building the foundation.

List Interface

This is one of the child interfaces of the Collection interface. It inhibits a list data structure to store ordered collection of objects. It can have duplicate values.

ArrayList

  • Uses a dynamic array to store duplicate elements of different data types.
  • It maintains the insertion order and is non-synchronized.
  • Stored elements can be randomly accessed.

Linked List

  • Uses a doubly linked list internally to store elements.
  • It maintains the insertion order and is non-synchronized.
  • Manipulation is fast since no shifting is required.

Vector

  • Uses a dynamic array to store elements.
  • Is synchronized and contains many methods that are not a part of Collection framework.

Stack

  • Subclass of Vector.
  • Implements last-in-first-out data structure. i.e — *stack*
  • Contains all vector class methods and also provides its own methods like push() and peek().

Queue Interface

1. An Ordered list used to hold elements which are about to be processed.
2. Queue maintains first-in-first-out order.

Priority Queue

  • Implements Queue interface.
  • It holds the elements/objects based on their priorities.
  • It does not allow null values to be stored.

Deque

  • Extends queue interface.
  • Remove and add elements from both the side.
  • Works as double-ended queue enabling operations on both ends.

ArrayDeque

  • Implements Deque interface.
  • Faster than ArrayList and Stack with no capacity restrictions.
Deque ad = new ArrayDeque()

Set Interface

  • extends the Collection interface.
  • Represents the unordered set of elements
  • It does not allow any duplicates, but at most a null value is allowed.

HashSet

  • Implements Set interface.
  • Represents the collection that uses a hash table for storage.
  • Uses hashing to store the elements.
  • It contains Unique items
Set<Integer> s1 = new HashSet<Integer>

LinkedHashSet

  • Represents LinkedList implementation of
  • Extends HashSet class and implements Set
  • Contains Unique elements
  • Maintains the insertion order and permits null elements.

SortedSet Interface

  • Alternate of Set interface that provides a total ordering of its elements.
  • Elements are arranges in the increasing order.

TreeSet

  • TreeSet Class implements the Set interface that uses a tree for storage.
  • Contains unique elements.
  • Access and retrieval time of TreeSet is quite fast.
  • Elements are stored in ascending order.
 SortedSet<String> set = new TreeSet();

--

--