Java Collections Introduction
In this page:
What is the Collections Framework?
The Collections Framework is Java's standardized set of interfaces and classes — List, Set, Map, and their implementations — for storing and manipulating groups of objects, replacing the need to hand-roll your own data structures.
Example: What is the Collections Framework?
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List<String> names = new ArrayList<>(); // standardized interface + implementation
names.add("Alice");
System.out.println(names);
}
}
Login to try C/C++/Java/PHP code in the editor
Core Interfaces
List, Set, and Queue are the three core collection interfaces, each defining different behavior around ordering and duplicates; Map sits alongside them, storing key-value pairs rather than a flat sequence of elements.
Example: Core Interfaces
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Set<String> set = new HashSet<>();
Map<String, Integer> map = new HashMap<>();
System.out.println(list.size() + " " + set.size() + " " + map.size());
}
}
Login to try C/C++/Java/PHP code in the editor
Primitives vs Objects
Collections only store objects, not primitives, so an int must be autoboxed into an Integer before it can go into a List<Integer> — this has real performance implications when storing millions of numeric values.
Example: Primitives vs Objects
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(); // int autoboxed into Integer
numbers.add(5);
int value = numbers.get(0);
System.out.println(value);
}
}
Login to try C/C++/Java/PHP code in the editor
The Collections Utility Class
The Collections utility class provides static helper methods — sort(), reverse(), max(), unmodifiableList() — that operate on any collection without you having to implement common algorithms yourself.
Example: The Collections Utility Class
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(List.of(3, 1, 2));
Collections.sort(numbers);
System.out.println(numbers);
}
}
Login to try C/C++/Java/PHP code in the editor
Advantages of Collections
Using the framework's interfaces instead of concrete classes directly (coding to List rather than ArrayList) lets you swap the underlying implementation later without touching the rest of your code.
Example: Advantages of Collections
import java.util.List;
import java.util.ArrayList;
public class Main {
static void printAll(List<String> items) { // coded to the interface, not ArrayList
for (String item : items) {
System.out.println(item);
}
}
public static void main(String[] args) {
printAll(new ArrayList<>(List.of("a", "b")));
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting