Collections Framework
Collections store groups of objects. Choosing the right one makes code simpler and faster.
On this page · 5 min · Intermediate
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 30, 2026
Related tutorials
In simple words#
A List is a queue at a counter (ordered, duplicates allowed). A Set is a guest list (no duplicates). A Map is a phone book (look up a number by name).
- List
- Ordered, allows duplicates (ArrayList).
- Set
- Unique elements (HashSet).
- Map
- Key → value pairs (HashMap).
Learning objectives#
- Use List, Set and Map
- Choose the right implementation
- Iterate and sort collections
- Create immutable collections
Explanation#
List: ArrayList (default, fast index access), LinkedList (fast insert at ends).
Set: HashSet (fast, unordered), LinkedHashSet (insertion order), TreeSet (sorted).
Map: HashMap (fast, unordered), LinkedHashMap (insertion order), TreeMap (sorted keys).
List.of, Set.of and Map.of create immutable collections. Program to interfaces: List<String> names = new ArrayList<>();
Code example: Orders, unique cities and stock lookup#
import java.util.*;
List<String> orders = new ArrayList<>(List.of("Laptop", "Mouse", "Laptop", "Keyboard"));
orders.add("Monitor");
Collections.sort(orders);
System.out.println(orders);
Set<String> cities = new TreeSet<>(List.of("Pune", "Delhi", "Pune", "Chennai"));
System.out.println(cities);
Map<String, Integer> stock = new HashMap<>();
stock.put("Laptop", 8);
stock.put("Mouse", 120);
stock.merge("Laptop", -2, Integer::sum); // sold 2 laptops
System.out.println("Laptops left: " + stock.get("Laptop"));
System.out.println("Keyboard: " + stock.getOrDefault("Keyboard", 0));
for (Map.Entry<String, Integer> e : stock.entrySet())
System.out.println(e.getKey() + " → " + e.getValue());
[Keyboard, Laptop, Laptop, Monitor, Mouse] [Chennai, Delhi, Pune] Laptops left: 6 Keyboard: 0 Laptop → 6 Mouse → 120
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- The ArrayList keeps duplicates; Collections.sort orders it.
- TreeSet removes duplicates and sorts.
- HashMap stores stock; merge updates a value.
- getOrDefault avoids nulls for missing keys.
Real-world use case#
A shopping cart uses a Map<productId, quantity>; a recommendation system uses a Set to avoid showing duplicates.
Common mistakes#
They are immutable — add() throws UnsupportedOperationException.
Throws ConcurrentModificationException. Use removeIf or an Iterator.
Best practices#
- Declare with interfaces (List, Map)
- ArrayList and HashMap as defaults
- Immutable collections for fixed data
- removeIf for filtering in place
Remember this#
List = order + duplicates. Set = unique. Map = key → value.
Hash = fast, Tree = sorted, Linked = insertion order.
List.of / Map.of are immutable.
Practice exercise#
Your task: Count how many times each word appears in a list.
Show solution
Map<String, Integer> counts = new HashMap<>();
for (String w : words) counts.merge(w, 1, Integer::sum);
Mini quiz#
Which collection keeps keys sorted?
Want more? Practise all Java MCQs · Java interview questions
My notes#
Saved in this browser.