Java Collections Framework

The Java Collections Framework is a group of interfaces and classes for storing and working with groups of objects. It gives you standard types such as List, Set, Map, and Queue so you can choose the right structure for each job.

Collections are part of java.util. Most collection classes use generics, such as ArrayList<String>, to say what type of object the collection stores.

Why Collections Matter

Arrays are useful when you know the size and want simple indexed storage. Collections are more flexible. Many of them can grow and shrink, provide helpful methods, and express different rules about ordering, duplicates, and lookup.

For example, use a List when order matters, a Set when duplicates should be ignored, and a Map when each value should be found by a key.

Main Collection Types

Type Common class Use
List ArrayList Stores ordered values and allows duplicates.
Set HashSet Stores unique values only.
Map HashMap Stores key-value pairs.
Queue LinkedList Stores values waiting to be processed.

List, Set, And Map Example

This example uses three common collection types. Notice how each type has different behavior.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        List<String> tasks = new ArrayList<>();
        tasks.add("Study");
        tasks.add("Practice");
        tasks.add("Study");

        Set<String> uniqueTasks = new HashSet<>(tasks);

        Map<String, Integer> scores = new HashMap<>();
        scores.put("Mia", 95);
        scores.put("Noah", 88);

        System.out.println(tasks);
        System.out.println(uniqueTasks.contains("Practice"));
        System.out.println(scores.get("Mia"));
    }
}

Output:

[Study, Practice, Study]
true
95

The List keeps all three values, including the repeated Study. The Set is useful for checking unique values. The Map gets Mia’s score by using the key "Mia".

Iterating Through Collections

A for-each loop works with most collection types. For maps, you can loop through keys, values, or entries. An entry contains one key-value pair.

import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, String> capitals = new LinkedHashMap<>();
        capitals.put("France", "Paris");
        capitals.put("Japan", "Tokyo");

        for (Map.Entry<String, String> entry : capitals.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

Output:

France: Paris
Japan: Tokyo

This example uses LinkedHashMap, which keeps insertion order. A regular HashMap does not guarantee insertion order. If your program needs sorted keys, use TreeMap.

Collections Utility Methods

The Collections class contains static utility methods for collection objects. For example, Collections.sort() sorts a list, Collections.reverse() reverses it, and Collections.max() finds the largest value.

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

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(4);
        numbers.add(1);
        numbers.add(7);

        Collections.sort(numbers);
        System.out.println(numbers);
        System.out.println(Collections.max(numbers));
    }
}

Output:

[1, 4, 7]
7

Collections.sort() changes the original list. It does not create a separate sorted copy.

Choosing A Collection

  • Use ArrayList when you need an ordered, resizable list.
  • Use HashSet when each value should appear only once.
  • Use HashMap when you want to look up values by keys.
  • Use LinkedList or another Queue implementation when values should be processed in line.

Takeaway: the Java Collections Framework gives you ready-made data structures with clear rules, so choose the collection whose behavior matches the problem you are solving.