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.
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:
List, Set and Queue extend the root Collection interface. Map is separate, because it holds pairs rather than single elements.
The main implementations#
A quick tour#
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
}
}[Ravi, Asha, Ravi]
[Asha, Ravi]
21 false
2Declare with the interface type#
Write the interface on the left and the implementation on the right:
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:
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");
}
}
}3 91
List.of creates an unmodifiable listTo 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:
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"));
}
}[3, 7, 19, 25, 42]
[42, 25, 19, 7, 3]
42 3
2Common operations on any Collection#
Choosing a collection#
Common mistakes#
Key takeaway#
Finished this lesson?