Collections in Java

Last Updated : 8 Apr 2026

Collections are used to store and manage a group of objects. They make it easy to perform common tasks like adding, removing, searching, and sorting data.

In this chapter, you will learn what collections are, how the collection framework works, and why it is important in Java.

What is Collection?

A Collection represents a group of objects. It is used to store multiple values in a single unit so that they can be managed easily.

What is a framework?

A framework provides a ready-made structure of classes and interfaces for building software applications efficiently. It simplifies adding new features by offering reusable components that perform similar tasks, eliminating the need to create a framework from scratch for each new project. This approach enhances object-oriented design that makes development quicker, more consistent, and reliable.

  • It provides a ready-made structure
  • It contains classes and interfaces
  • It helps in faster and easier development

What is Collection Framework?

The Collection Framework is a set of classes and interfaces used to store and manage data. It provides different data structures like lists, sets, and queues.

It includes:

  • Interfaces and their implementations (classes)
  • Algorithms for operations like sorting and searching

Types of Collections in Java

Java Collection Framework provides different types of interfaces and classes:

Interfaces:

  • List
  • Set
  • Queue
  • Deque

Classes:

  • ArrayList
  • Vector
  • LinkedList
  • PriorityQueue
  • HashSet
  • LinkedHashSet
  • TreeSet

Why Collection Framework?

Before the Collection Framework, Java used arrays, vectors, and hash tables. These did not follow a common structure, so they were hard to use and manage.

The Collection Framework solves this problem by providing a standard way to work with data. It makes code easier to write, understand, and reuse.

Hierarchy of Collection Framework

Let's see the hierarchy of Collection framework. The java.util package contains all the classes and interfaces for the Collection framework.

The Java Collections Framework is structured around key interfaces-Collection, List, Set, Queue, and Map. Each tailored for specific data management tasks. Implementations like ArrayList, HashSet, and HashMap offer practical solutions for working with these collections, giving Java developers a versatile set of tools for efficient data handling.

Hierarchy of Java Collection framework
  • Class: A class is a blueprint from which individual objects are created. It encapsulates data for objects through fields (attributes) and defines behavior via methods. Classes support inheritance, allowing one class to inherit the properties and methods of another, facilitating code reuse and polymorphism.
  • Interface: An interface is a reference type in Java that can contain constants and abstract methods (methods without a body). Interfaces specify what a class must do but not how it does it, enforcing a set of methods that the class must implement. Interfaces are used to achieve abstraction and multiple inheritance in Java.

Collection Interface

The Collection interface is the interface which is implemented by all the classes in the collection framework. It declares the methods that every collection will have. In other words, we can say that the Collection interface builds the foundation on which the collection framework depends.

Some of the methods of Collection interface are Boolean add (Object obj), Boolean addAll (Collection c), void clear(), etc. that are implemented by all the subclasses of Collection interface.

Example

The following code snippet demonstrates how to use the Collection interface to store and display elements.

Output:

Rahul
Priya
Amit

Iterator Interface

The Iterator interface is used to traverse elements of a collection one by one. It allows only forward direction traversal. It also provides methods to remove elements while iterating.

Example

The following code snippet demonstrates how to use the Iterator interface to traverse elements one by one.

Output:

Rahul
Priya
Amit

Iterable Interface

The Iterable interface is the root interface for all the collection classes. The Collection interface extends the Iterable interface, and therefore, all the subclasses of the Collection interface also implement the Iterable interface.

It contains only one abstract method. i.e.,

It returns the iterator over the elements of type T.

Example

The following code snippet shows how Iterable is used to iterate over elements.

Output:

Rahul
Priya
Amit

List Interface

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

Various methods in List interface can be used to insert, delete, and access the elements from the list. The classes that implement the List interface are given below:

  • ArrayList
  • LinkedList
  • Vector
  • Stack

List Interface Example

The following example demonstrates how to use the List interface to store ordered elements.

Output:

[Rahul, Priya, Amit]

ArrayList

The ArrayList class implements the List interface. It uses a dynamic array to store the duplicate elements of different data types. The ArrayList class maintains the insertion order and is non-synchronized. The elements stored in the ArrayList class can be randomly accessed. Consider the following example.

ArrayList Example

The following example shows how to use ArrayList to store and access elements.

Compile and Run

Output:

Priya

LinkedList

LinkedList implements the Collection interface. It uses a doubly linked list internally to store the elements. It can store the duplicate elements. It maintains the insertion order and is not synchronized. In LinkedList, the manipulation is fast because no shifting is required. Consider the following example.

LinkedList Example

In this example, a LinkedList is created and traversed using an Iterator to print each element, including duplicate values.

Compile and Run

Output:

Lucy
Peter
Lucy
John

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. Consider the following example.

Vector Example

In this example, a Vector is created and traversed using an Iterator to print each element in the collection.

Compile and Run

Output:

Apple
Banana
Orange
Plum

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. Consider the following example.

Stack Example

In this example, a Stack is used to store elements, one element is removed using pop(), and the remaining elements are traversed using an Iterator to display them.

Compile and Run

Output:

CPU
Monitor
Mouse
Keyboard

Queue Interface

The queue interface maintains the first-in-first-out order. It can be defined as an ordered list that is used to hold the elements that are about to be processed. There are various classes, like PriorityQueue, Deque, and ArrayDeque, which implement the Queue interface.

Queue interface can be instantiated as:

Various classes implement the Queue interface; some of them are given below:

  • PriorityQueue
  • Deque
  • ArrayDeque

Queue Interface Example

The following example demonstrates how Queue follows FIFO order.

Output:

Rahul

PriorityQueue

The PriorityQueue class implements the Queue interface. It holds the elements or objects which are to be processed by their priorities. PriorityQueue does not allow null values to be stored in the queue. Consider the following example.

PriorityQueue Example

In this example, a PriorityQueue is used to store elements, access the head using element() and peek(), iterate through elements, and demonstrate removal using remove() and poll().

Compile and Run

Output:

head:Alice
head:Alice
iterating the queue elements:
Alice
Daniel
Jones
Smith
after removing two elements:
Jones
Smith

Deque Interface

The Deque interface extends the Queue interface. In Deque, we can remove and add the elements from both sides. Deque stands for a double-ended queue, which enables us to perform the operations at both ends.

Deque Interface Example

The following example demonstrates adding elements from both ends.

Output:

[Rahul, Priya]

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 ends. ArrayDeque is faster than ArrayList and Stack and has no capacity restrictions. Consider the following example.

ArrayDeque Example

In this example, a Deque is created using ArrayDeque, elements are added, and then traversed using a for-each loop to display each element.

Compile and Run

Output:

Lucy
Andrew
Henery

Set Interface

Set Interface in Java is present in java.util package. It extends the Collection interface. It represents the unordered set of elements which does not allow us to store the duplicate items. We can store at most one null value in Set. HashSet, LinkedHashSet, and TreeSet implement set. The set can be instantiated as:

Set Interface Example

The following example shows how Set stores unique elements.

Output:

[Rahul, Priya]

HashSet

The HashSet class implements Set Interface. It represents the collection that uses a hash table for storage. Hashing is used to store the elements in the HashSet. It contains unique items. Consider the following example.

Hashset Example

In this example, a HashSet is created to store unique elements, and an Iterator is used to traverse and print each element.

Compile and Run

Output:

Johnson
Andrew
Mark
Peter

LinkedHashSet

The LinkedHashSet class represents the LinkedList implementation of the Set Interface. It extends the HashSet class and implements the Set interface. Like HashSet, it also contains unique elements. It maintains the insertion order and permits null elements. Consider the following example.

LinkedHashSet Example

In this example, a LinkedHashSet is used to store unique elements while maintaining insertion order, and an Iterator is used to traverse and print the elements.

Compile and Run

Output:

Peter
Jack
Johnson

SortedSet Interface

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

SortedSet Interface Example

The following example demonstrates sorted elements in ascending order.

Output:

[Amit, Priya, Rahul]

TreeSet

Java TreeSet class implements the Set interface that uses a tree for storage. Like HashSet, TreeSet also contains unique elements. However, the access and retrieval time of TreeSet is quite fast. The aspects in TreeSet are stored in ascending order. Consider the following example.

TreeSet Example

In this example, a TreeSet is used to store unique elements in sorted order, and an Iterator is used to traverse and print the elements.

Compile and Run

Output:

Davis
Donald
Thomas

Map Interface

The Map interface in Java is part of the Java Collections Framework. It represents a mapping between a set of keys and their corresponding values. A Map cannot contain duplicate keys; each key can map to at most one value. The Map interface is used to store key-value pairs, where each key is unique, and it provides an efficient way to retrieve, update, and manipulate data based on keys.

Syntax:

It has the following syntax:

Map Interface Example

The following example demonstrates storing key-value pairs.

Output:

{1=Rahul, 2=Priya, 3=Amit}

HashMap

HashMap in Java is a key-value data structure offering efficient data access via keys using hashing. Hashing converts large strings or other objects into smaller, consistent values for quick indexing and searching. HashMap implements the Map interface and is used for managing large datasets efficiently. Additionally, HashSet uses HashMap internally to store elements uniquely, demonstrating the utility of hashing in Java's collections framework for fast data retrieval and management.

HashMap Example

In this example, a HashMap is used to store key-value pairs, retrieve values, iterate through entries, remove elements, and check for the presence of a key.

Compile and Run

Output:

Value for 'Alice': 10Bob: 20
Alice: 10
Charlie: 30
Map contains key 'Bob'.

Next TopicJava ArrayList