Collections Framework Overview
Site Admin
· 11 Sep 2026
· 11 views
Data Structures, Ready-Made
The Collections Framework in java.util provides ready-made implementations of the most useful data structures, so you do not have to write linked lists or hash maps yourself.
The Three Main Interfaces
| Interface | What it is | Popular examples |
|---|---|---|
| List | Ordered sequence, allows duplicates | ArrayList, LinkedList |
| Set | Unique elements, no duplicates | HashSet, TreeSet |
| Map | Key-value pairs | HashMap, TreeMap |
List in Practice
import java.util.*;
List<String> cities = new ArrayList<>();
cities.add("Delhi");
cities.add("Pune");
cities.add("Delhi"); // duplicates allowed
System.out.println(cities.get(1)); // Pune
System.out.println(cities.size()); // 3Set in Practice
Set<String> unique = new HashSet<>();
unique.add("apple");
unique.add("banana");
unique.add("apple"); // ignored - no duplicates
System.out.println(unique); // [apple, banana]Map in Practice
Map<String, Integer> scores = new HashMap<>();
scores.put("Asha", 92);
scores.put("Bimal", 85);
System.out.println(scores.get("Asha")); // 92
System.out.println(scores.containsKey("Bimal")); // trueChoosing the Right Collection
- ArrayList: fast random access, fast append.
- LinkedList: fast insert/delete at ends for big lists.
- HashSet: fastest uniqueness checks; no order.
- TreeSet: unique elements kept sorted.
- HashMap: fastest key lookup; unordered.
- TreeMap: keys kept in sorted order.
Iterating
for (String c : cities) {
System.out.println(c);
}
for (Map.Entry<String, Integer> e : scores.entrySet()) {
System.out.println(e.getKey() + " = " + e.getValue());
}
- Lists allow duplicates, Sets do not, Maps pair keys with values.
- Choose the simplest structure that meets your needs.
- Use generic types
List<String>to keep collections type safe.