Collection Framework in Java

Neeraj Tiwari
7 min readAug 29, 2021

What are Java collections?

Java collections refer to a collection of individual objects that are represented as a single unit. You can perform all operations such as searching, sorting, insertion, manipulation, deletion, etc., on Java collections just like you do it on data.

Now Let’s move ahead and understand other aspects of it:

What is a Java Collection Framework?

Java Collection Framework is a ready made architecture to store and manipulate group of objects. A Java collection framework provides the following:

  • Interfaces
  • Classes
  • Algorithm

Interfaces: An interface in Java is a blueprint of a class. It is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. They allow Java collections to be manipulated independently from the details of their representation. Also, they form a hierarchy in object-oriented programming languages.

Classes: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can’t be physical.

Algorithm: Algorithm refers to the methods which are used to perform operations such as searching and sorting, on objects that implement collection interfaces. Algorithms are polymorphic in nature as the same method can be used to take many forms or you can say perform different implementations of the Java collection interface.

Before starting about Java collection framework, let us first think about why we use Java collection.

Why use Java collection?

There are several benefits of using Java collections such as:

  • Reducing the effort required to write the code by providing useful data structures and algorithms
  • Java collections provide high-performance and high-quality data structures and algorithms thereby increasing the speed and quality
  • Unrelated APIs can pass collection interfaces back and forth
  • Decreases extra effort required to learn, use, and design new API’s
  • Supports re-usability of standard data structures and algorithms

Now let’s see the hierarchy of Java collection framework.

Java Collection Framework Hierarchy

As we have learned Java collection framework includes interfaces and classes. The java.util package contains all the classes and interfaces for the Collection framework.

Collection Framework Hierarchy

In the above image, blue part refers to the different interfaces and the yellow part defines the class. Now, let us understand these components in detail.

Iterator Interface:

Iterator interface provides the facility of iterating the elements in a forward direction only.

There are only three methods in the Iterator interface. They are:

  1. public boolean hasNext(): It returns true if the iterator has more elements otherwise it returns false.
  2. public Object next(): It returns the element and moves the cursor pointer to the next element.
  3. public void remove(): It removes the last elements returned by the iterator. It is less used.

There are three components that extend the collection interface i.e List, Queue and Sets. Let’s learn about them in detail:

Collection Interface:

Collection interface builds the foundation on which the collection framework depends. The Collection interface is the interface which is implemented by all the classes in the collection framework.

List Interface: List interface is the child interface of Collection interface. It inhibits a list type data structure in which we can store the ordered collection of objects. It can have duplicate values. List interface is implemented by the classes ArrayList, LinkedList, Vector, and Stack.

Let’s see each of them:

ArrayList: ArrayList is the implementation of List Interface where the elements can be dynamically added or removed from the list. Also, the size of the list is increased dynamically if the elements are added more than the initial size.

Syntax: ArrayList<data-type> object = new ArrayList<> ();

Example:

Output:

LinkedList: Linked List is a sequence of links which contains items. Each link contains a connection to another link.

Syntax: LinkedList<data-type> object = new LinkedList<data-type>();

Example:

Output:

Vector: Vector uses a dynamic array to store the data elements. It is similar to ArrayList. However, It is synchronized and contains many methods that are not the part of Collection framework. Also, the vector is not limited to a specific size, it can shrink or grow automatically whenever required.

Syntax: Vector<data-type> object = new Vector<data-type>();

Example:

Output:

Stack: The stack is the subclass of Vector. It implements the last-in-first-out data structure, i.e., Stack. The stack contains all of the methods of Vector class and also provides its methods like boolean push(), boolean peek(), boolean push(object o), which defines its properties.

Syntax: Stack<data-type> object = new Stack<data-type>();

Example:

Output:

Queue Interface: Queue in Java follows a FIFO approach i.e. it orders the elements in First In First Out manner. In a queue, the first element is removed first and last element is removed in the end. Each basic method exists in two forms: one throws an exception if the operation fails, the other returns a special value.

Queue Hierarchy in Collection

PriorityQueue: The PriorityQueue class implements the Queue interface. It holds the elements or objects which are to be processed by their priorities. PriorityQueue doesn’t allow null values to be stored in the queue.

Syntax: Queue<data-type> queue1 = new PriorityQueue<data-type>();

Example:

Output:

ArrayDeque: ArrayDeque class implements the Deque interface. It facilitates us to use the Deque. Unlike queue, we can add or delete the elements from both the ends. ArrayDeque is faster than ArrayList and Stack and has no capacity restrictions.

Syntax: Queue<data-type> queue2 = new ArrayDeque<data-type>();

Example:

Output:

Set Interface: A Set refers to a collection that cannot contain duplicate elements. It is mainly used to model the mathematical set abstraction. Set has its implementation in various classes such as HashSet, TreeSet and LinkedHashSet.

HashSet: Java HashSet class creates a collection that use a hash table for storage. HashSet only contain unique elements and it inherits the AbstractSet class and implements Set interface. Also, it uses a mechanism hashing to store the elements.

Syntax: HashSet<data-type> object = new HashSet<data-type>();

Example:

Output:

Linked Hashset : Java LinkedHashSet class is a Hash table and Linked list implementation of the set interface. It contains only unique elements like HashSet. Linked HashSet also provides all optional set operations and maintains insertion order.

Syntax: LinkedHashSet<data-type> set = new LinkedHashSet<data-type>();

Example:

Output:

SortedSet: SortedSet is the alternate of Set interface that provides a total ordering on its elements. The elements of the SortedSet are arranged in the increasing (ascending) order. The SortedSet provides the additional methods that inhibit the natural ordering of the elements.

TreeSet: TreeSet class implements the Set interface that uses a tree for storage. The objects of this class are stored in the ascending order. In TreeSet class, access and retrieval time are faster.

Syntax: TreeSet<data-type> set = new TreeSet<data-type>();

Example:

Output:

--

--