Runtime AtlasJava platform fieldbook
Java · Language to RuntimeView Markdown source

Collections and data structures

Choose a collection from required ordering, uniqueness, lookup, mutation, concurrency, and memory behavior.

Core interfaces

Type Main property
List Ordered sequence, duplicates allowed
Set Unique elements
Map Key-to-value association
Queue / Deque Processing order and double-ended operations

ArrayList is the usual list default. HashSet and HashMap use hash codes and equality. TreeSet and TreeMap maintain sorted order. LinkedHashMap maintains encounter order. Java 21 introduced sequenced collection interfaces for first/last and reversed views.

Equality contract

If two objects are equal, they must have the same hash code. Fields used in equals/hashCode should not change while the object is a hash key. Records generate value-based equality from their components.

Mutability

List.of creates an unmodifiable list and rejects null elements. Collections.unmodifiableList creates a read-only view of a backing list; the backing list can still change. List.copyOf creates an unmodifiable snapshot subject to element mutability.

Iteration

Do not structurally modify most collections while using a for-each loop except through the iterator's supported removal. Concurrent modification detection is best-effort, not a thread-safety mechanism.

Concurrent choices

Use ConcurrentHashMap for highly concurrent map access, CopyOnWriteArrayList for read-heavy/small write workloads, and blocking queues for producer-consumer coordination. Measure instead of synchronizing every collection blindly.

Feynman check

A list is a numbered shelf, a set is a guest list with no duplicates, a map is a dictionary, and a queue is a line of work.

Runtime AtlasIndependent study material · verify production details in official Java documentation