Collections Framework in Java

Subject: Data Structures and Algorithms · Language: Java · Level: beginner · 6 min read

Understand ArrayList, LinkedList, HashSet, HashMap, Queue, PriorityQueue, and when to use them.

The Collections Framework provides ready-made classes and interfaces to store and process groups of data in Java. Instead of building every data structure from scratch, Java gives commonly used structures such as `ArrayList`, `LinkedList`, `HashSet`, `HashMap`, `Queue`, and `PriorityQueue`. Collections are useful because real programs rarely work with only one value. We usually store lists of numbers, names, records, unique values, key-value pairs, or tasks waiting to be processed.

For DSA, collections save time during coding rounds. But a student should still understand the basic logic behind arrays, linked lists, stacks, queues, sets, and maps. Collections make implementation faster, but they should not hide the concept.

Collection vs Collections

`Collection` is an interface that represents a group of objects. `Collections` is a utility class that provides helper methods such as sorting, reversing, finding maximum, and finding minimum. This small spelling difference is important. `Collection` is a type, while `Collections` gives ready-made operations.

ArrayList

`ArrayList` is a resizable array. Unlike a normal array, its size can grow when new elements are added. Use `ArrayList` when you need fast access by index and flexible size. It is good for storing input values, maintaining a dynamic list, and accessing elements using positions.

```java

ArrayList<Integer> list = new ArrayList<>();

list.add(10);

list.add(20);

System.out.println(list.get(0));

```

Here, `add` inserts values and `get(0)` reads the first value. `ArrayList` is one of the most commonly used collections in Java DSA.

LinkedList

`LinkedList` stores data as connected nodes. It can be used as a list, stack, or queue depending on the operations you use. In Java library usage, `LinkedList` is helpful when frequent insertion or deletion is needed near the beginning or end. For learning DSA, it is still important to understand how nodes and references work internally.

HashSet

`HashSet` stores unique values. It does not allow duplicate elements. It is useful when you need to check whether a value already exists. For example, duplicate detection, visited tracking, and unique element problems can often use `HashSet`.

```java

HashSet<Integer> set = new HashSet<>();

set.add(5);

set.add(5);

System.out.println(set.size());

```

The size is `1` because duplicate value `5` is stored only once.

HashMap

`HashMap` stores data in key-value pairs. A key is used to find its value quickly. It is useful for frequency counting, fast lookup, and mapping one value to another. For example, counting how many times each number appears in an array is a common `HashMap` use case.

```java

HashMap<String, Integer> marks = new HashMap<>();

marks.put("Rahul", 85);

System.out.println(marks.get("Rahul"));

```

Here, `Rahul` is the key and `85` is the value.

Queue and PriorityQueue

`Queue` follows FIFO, which means first in, first out. The element inserted first is removed first, just like a normal line of people. `PriorityQueue` removes elements based on priority, not just insertion order. By default, Java's `PriorityQueue` gives the smallest value first for numbers.

Queues are used in BFS, scheduling, and level order traversal. Priority queues are used in problems where the smallest or largest value must be accessed repeatedly.

Choosing the Right Collection

Use `ArrayList` when you need a flexible list with index access. Use `HashSet` when uniqueness and fast existence checking matter. Use `HashMap` when each value needs a related count or mapping. Use `Queue` when FIFO order is needed. Use `PriorityQueue` when priority-based removal is needed.

Choosing the correct collection can make a solution simpler and faster. In placement coding, this choice often decides whether a solution is clean enough for the time limit.

Example Program

```java

import java.util.*;

public class Main {

public static void main(String[] args) {

ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(10);

numbers.add(20);

numbers.add(30); for (int value : numbers) {

System.out.print(value + " ");

}

System.out.println();

}

}

``` This program creates an `ArrayList`, adds three values, and prints them using an enhanced `for` loop. The same style is commonly used when processing dynamic input values.

Practice Question

Create an `ArrayList` of integers, add five numbers, and print only the even numbers. Then use a `HashSet` to print how many unique numbers are present.

More Java Lessons

Browse all PrepCampus study materials