Skip to content
Laviyx Learn

Try

Java course menu

Java Collections Framework Overview: List, Set, Map and Queue

An overview of the Java Collections Framework: the Collection, List, Set, Queue and Map interfaces, their main implementations (ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap, ArrayDeque, PriorityQueue), immutable collections with List.of, the Collections utility class, and how to choose.

Intermediate7 min readUpdated
On this page

Arrays have a fixed size and few built-in operations. The Java Collections Framework (in java.util) provides ready-made, well-tested data structures that grow as needed and come with rich methods. Knowing which one to choose is one of the most practical Java skills.

The main interfaces#

The framework is organized around interfaces, each describing a kind of collection:

InterfaceWhat it isDuplicates?Order
ListAn ordered sequence, accessed by indexYesInsertion order
SetA collection of unique elementsNoDepends on the implementation
Queue / DequeElements waiting to be processedYesFIFO, LIFO or priority
MapKey-value pairs with unique keysKeys: noDepends on the implementation

List, Set and Queue extend the root Collection interface. Map is separate, because it holds pairs rather than single elements.

The main implementations#

InterfaceImplementationNotes
ListArrayListDefault choice. Fast access by index; growable array
LinkedListFast insert/remove at both ends; slow index access
SetHashSetDefault choice. O(1) operations, no order
LinkedHashSetKeeps insertion order
TreeSetKeeps elements sorted; O(log n)
MapHashMapDefault choice. O(1) operations, no order
LinkedHashMapKeeps insertion order
TreeMapKeeps keys sorted; O(log n)
Queue / DequeArrayDequeStack and queue operations at both ends
PriorityQueueAlways gives the smallest (or highest-priority) element first

A quick tour#

Java
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>(List.of("Ravi", "Asha", "Ravi"));
        Set<String> unique = new TreeSet<>(names);
        Map<String, Integer> ages = new TreeMap<>(Map.of("Asha", 21, "Ravi", 25));
        Deque<Integer> stack = new ArrayDeque<>();
        stack.push(1);
        stack.push(2);

        System.out.println(names);        // keeps duplicates and order
        System.out.println(unique);       // unique and sorted
        System.out.println(ages.get("Asha") + " " + ages.containsKey("John"));
        System.out.println(stack.pop());  // last in, first out
    }
}
Output
[Ravi, Asha, Ravi]
[Asha, Ravi]
21 false
2

Declare with the interface type#

Write the interface on the left and the implementation on the right:

Java
List<String> names = new ArrayList<>();       // not: ArrayList<String> names
Map<String, Integer> stock = new HashMap<>();

The rest of your code then depends only on List or Map, so switching to another implementation later changes one line. The empty <> (the "diamond") lets Java infer the type arguments.

Collections hold objects only#

Type arguments must be classes, so primitives are stored through their wrapper classes: List<Integer>, not List<int>. Autoboxing converts automatically; see Wrapper Classes.

Immutable collections#

List.of, Set.of and Map.of (Java 9+) create small unmodifiable collections. They are perfect for constants and test data, and they reject null elements:

Java
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        List<String> days = List.of("Mon", "Tue", "Wed");
        Map<String, Integer> codes = Map.of("IN", 91, "JP", 81);

        System.out.println(days.size() + " " + codes.get("IN"));
        try {
            days.add("Thu");
        } catch (UnsupportedOperationException e) {
            System.out.println("List.of creates an unmodifiable list");
        }
    }
}
Output
3 91
List.of creates an unmodifiable list

To get a modifiable copy, wrap it: new ArrayList<>(List.of(...)).

The Collections utility class#

java.util.Collections provides static helpers for collections, much like Arrays does for arrays:

Java
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(List.of(42, 7, 19, 3, 25));

        Collections.sort(nums);
        System.out.println(nums);
        Collections.reverse(nums);
        System.out.println(nums);
        System.out.println(Collections.max(nums) + " " + Collections.min(nums));
        System.out.println(Collections.frequency(List.of("a", "b", "a"), "a"));
    }
}
Output
[3, 7, 19, 25, 42]
[42, 25, 19, 7, 3]
42 3
2

Common operations on any Collection#

MethodMeaning
add(x), remove(x)Add or remove an element
size(), isEmpty()Count and emptiness
contains(x)Membership test
addAll(other), removeAll(other), retainAll(other)Bulk operations
removeIf(predicate)Remove every element matching a condition
stream()Start a stream pipeline (see Streams)
for (T x : collection)Iterate

Choosing a collection#

You need to…Use
Keep an ordered list, access by positionArrayList
Look up values by a keyHashMap (TreeMap if you need sorted keys)
Check "have I seen this?" or remove duplicatesHashSet (TreeSet for sorted output)
Process items in arrival order (FIFO)ArrayDeque as a Queue
Undo history, matching brackets (LIFO)ArrayDeque as a stack
Always take the most urgent itemPriorityQueue

Common mistakes#

Key takeaway#

Finished this lesson?