Java · Core APIs

Collections Framework

Collections store groups of objects. Choosing the right one makes code simpler and faster.

Intermediate 5 min Lesson 18 of 31Examples target Java 21 LTS (they also run on Java 25).
Before you start:ArraysGenerics
0/31 in Java
On this page · 5 min · Intermediate

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#

JavaOrders, unique cities and stock lookup Illustrative
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());
Expected output
[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#

  1. The ArrayList keeps duplicates; Collections.sort orders it.
  2. TreeSet removes duplicates and sorts.
  3. HashMap stores stock; merge updates a value.
  4. 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#

Modifying List.of results

They are immutable — add() throws UnsupportedOperationException.

Removing items inside a for-each

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
Java Illustrative
Map<String, Integer> counts = new HashMap<>();
for (String w : words) counts.merge(w, 1, Integer::sum);

Mini quiz#

Collections Framework quizMultiple choice Question 1 of 1

Which collection keeps keys sorted?

Score so far: 0/0

Want more? Practise all Java MCQs · Java interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.