Chapter 46 of 57

Collections in Java

As we start working with larger amounts of data, storing everything in simple variables or arrays can become difficult. We may need to store a list of names, remove duplicates, search for values, or associate one value with another.

Java provides the Collections Framework to make these tasks easier.

In simple words, the Collections Framework gives us ready-made classes and interfaces for storing and working with groups of objects.

You have already seen some of them:

ArrayList
HashSet
HashMap

These are all commonly used when working with collections.

Why Do We Need Collections?

Suppose we want to store some student names.

With separate variables, we might write:

String student1 = "John";
String student2 = "Jason";
String student3 = "Alex";

This becomes inconvenient when the number of students increases.

An array is better:

String[] students = {"John", "Jason", "Alex"};

But arrays have a fixed size.

Collections give us more flexible options.

For example, with an ArrayList:

ArrayList<String> students = new ArrayList<>();

students.add("John");
students.add("Jason");
students.add("Alex");

We can easily add and remove elements as the program runs.

Main Collection Types

The most commonly used collection types are:

List
Set
Map

They are designed for different purposes.

List

A List stores elements in a sequence and allows duplicates.

A commonly used implementation is ArrayList.

ArrayList<String> names = new ArrayList<>();

names.add("John");
names.add("Jason");
names.add("John");

Here, "John" can appear twice.

A list also maintains the order of its elements.

John
Jason
John

You can access elements using their index:

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

Output:

John

Set

A Set is used when we want unique elements.

A commonly used implementation is HashSet.

HashSet<String> names = new HashSet<>();

names.add("John");
names.add("Jason");
names.add("John");

The second "John" is ignored.

So the set contains only unique values.

John
Jason

Unlike a list, a HashSet does not provide index-based access and does not guarantee a particular iteration order.

Map

A Map stores data as key-value pairs.

A commonly used implementation is HashMap.

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

marks.put("John", 85);
marks.put("Jason", 92);

Now we have:

John  → 85
Jason → 92

We can retrieve John's marks using his name:

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

Output:

85

A Map is useful when we want to associate one piece of information with another.

Collection vs Collections

There are two terms that can easily confuse beginners: Collection and Collections.

Collection is an interface that represents a group of objects. Interfaces such as List and Set are part of this collection hierarchy.

Collections is a utility class in Java that provides useful methods for working with collections.

For example:

Collections.sort(names);

can be used to sort a list.

We would import it with:

import java.util.Collections;

So remember:

Collection → an interface in the collection hierarchy

Collections → a utility class containing useful methods

ArrayList, HashSet, and HashMap

Let's quickly compare the three collections we've learned:

Collection

Stores

Duplicates

Index

ArrayList

Values

Allowed

Yes

HashSet

Unique values

Not allowed

No

HashMap

Key-value pairs

Keys must be unique

No

For example:

ArrayList:

John
Jason
John

HashSet:

John
Jason

HashMap:

John → 85
Jason → 92

Choosing the right collection depends on what your program actually needs.

Sorting a Collection

Java provides the Collections utility class with several useful methods.

For example, we can sort an ArrayList:

import java.util.ArrayList;
import java.util.Collections;

class Main {
    public static void main(String[] args) {

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

        numbers.add(50);
        numbers.add(10);
        numbers.add(30);
        numbers.add(20);

        Collections.sort(numbers);

        System.out.println(numbers);
    }
}

Output:

[10, 20, 30, 50]

The Collections.sort() method arranges the elements in ascending order.

Searching in a Collection

We can also use useful methods such as contains().

For example:

ArrayList<String> names = new ArrayList<>();

names.add("John");
names.add("Jason");
names.add("Alex");

System.out.println(names.contains("Jason"));

Output:

true

This allows us to easily check whether a value exists in the list.

Using Collections with Loops

Collections are commonly used with loops.

For example:

ArrayList<String> names = new ArrayList<>();

names.add("John");
names.add("Jason");
names.add("Alex");

for (String name : names) {
    System.out.println(name);
}

Output:

John
Jason
Alex

This enhanced for loop is a very common way to process collection elements.

Why Collections Are Important

In real Java applications, you'll work with collections constantly.

For example, an application might have:

List → list of products
Set → unique user IDs
Map → user ID → user details

Collections save us from implementing common data-storage operations ourselves. Java provides ready-made implementations that we can use depending on our requirements.

You'll also encounter other collection classes such as LinkedList, TreeSet, and TreeMap as you learn more about Java.

The main thing to remember is:

The Java Collections Framework provides ready-made ways to store, organize, search, and manipulate groups of objects.

The most important ones to understand initially are ArrayList for ordered data, HashSet for unique data, and HashMap for key-value data.