BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Collections and streams

Java language

Collections: List, Set, Map

Collections hold groups of objects.

Learn these first

At a glance

Mental model

A List is a numbered queue, a Set is a guest list where each name appears once, and a Map is a phone book from names to numbers.

What happens, in order
  1. 1ArrayList is the default list: fast access by index, slower to insert in the middleLinkedList is rarely the right choice.
  2. 2HashSet and HashMap look values up in constant time on average using hashCode and equals; their iteration order is not definedLinkedHashMap keeps insertion order, TreeMap keeps keys sorted.
  3. 3List.of(...), Set.of(...) and Map.of(...) create immutable collections; changing them throws UnsupportedOperationException
  4. 4Common Map idioms: getOrDefault, computeIfAbsent(key, k -> new ArrayList<>()).add(v), merge(key, 1, Integer::sum)
Remember
Watch out
Mutating a list during for-eachMutable keysMissing hashCode when overriding equalsSharing a plain HashMap across threadsAssuming HashMap iterates in insertion order
Example (highlighted lines explain it)
Map<String, List<Order>> byCustomer = new HashMap<>();for (Order o : orders) {    byCustomer.computeIfAbsent(o.getCustomerId(), k -> new ArrayList<>()).add(o);}Map<String, Integer> counts = new TreeMap<>();for (String word : words) {    counts.merge(word, 1, Integer::sum);}
  1. line 1 uses Map, the core of Collections: List, Set, Map
  2. line 3 uses ArrayList, the core of Collections: List, Set, Map
  3. line 5 uses Map, the core of Collections: List, Set, Map
Read the full explanation (about 1 min)

Collections hold groups of objects. A List keeps order and allows duplicates, a Set holds each value once, and a Map looks values up by key.

A mental model

A List is a numbered queue, a Set is a guest list where each name appears once, and a Map is a phone book from names to numbers.

How it works

ArrayList is the default list: fast access by index, slower to insert in the middle. LinkedList is rarely the right choice.

HashSet and HashMap look values up in constant time on average using hashCode and equals; their iteration order is not defined. LinkedHashMap keeps insertion order, TreeMap keeps keys sorted.

List.of(...), Set.of(...) and Map.of(...) create immutable collections; changing them throws UnsupportedOperationException.

Common Map idioms: getOrDefault, computeIfAbsent(key, k -> new ArrayList<>()).add(v), merge(key, 1, Integer::sum).

Going deeper

Keys of a HashMap or elements of a HashSet must have consistent equals and hashCode, and must not change while stored. A mutable key whose hash changes becomes unfindable.

Removing from a list while looping over it with a for-each throws ConcurrentModificationException; use an Iterator, removeIf or collect first.

Standard collections are not thread safe. Share them between threads only with synchronisation, or use the concurrent versions.

Return immutable or copied collections from public methods, so callers cannot change your internal state.

Under the hood

HashMap is an array of buckets; each key's hash picks a bucket, and colliding entries chain in a list that becomes a balanced tree when it gets long (8+ entries). It resizes by doubling at a 0.75 load factor.

Common mistakes

  • Mutating a list during for-each.
  • Mutable keys.
  • Missing hashCode when overriding equals.
  • Sharing a plain HashMap across threads.
  • Assuming HashMap iterates in insertion order.

How Unravel recognises it

When you paste code, Unravel looks for these signs of collections: list, set, map. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Builds on this

Frequently asked questions

What is Collections: List, Set, Map?

Collections hold groups of objects. A List keeps order and allows duplicates, a Set holds each value once, and a Map looks values up by key.

What are the most common mistakes with Collections: List, Set, Map?

Mutating a list during for-each. Mutable keys. Missing hashCode when overriding equals. Sharing a plain HashMap across threads. Assuming HashMap iterates in insertion order.

How can I see Collections: List, Set, Map in real code?

Paste your own Java into Unravel and it will point out where collections: list, set, map appears, explain how it works at the detail level you choose, and draw the flow of the code around it.