Lists, Sets and Maps
- List. A List is an ordered collection: ArrayList backs it with a growable array, the everyday default.
- Set. A Set holds each element once: HashSet answers contains in constant time via hashing.
- Map. A Map pairs keys with values: HashMap is the lookup table most Java programs are built around.
Generic Types
- Generics. Generics parameterise types: List<Order> can only hold orders, checked at compile time, no casts.
- Type erasure. Generics are erased at runtime: the JVM sees plain List, which is why new T() cannot compile.
- Bounded types. A bound like <T extends Comparable<T>> demands capability: sort only what can compare itself.
Choosing Appropriate Collections
- Choosing by operation. Choose by the hot operation: random access wants ArrayList, membership wants HashSet, ordering wants TreeMap.
- equals and hashCode. Hash collections trust equals and hashCode together: override one without the other and elements vanish.
- Immutable collections. List.of and Map.of build immutable collections: safe to share, impossible to corrupt.
Before and after this level
- Before: Level 3: Interfaces & Polymorphism: Interfaces and Abstract Classes, Dynamic Dispatch, Designing Flexible Object Models.
- Next: Level 5: Exceptions: Checked and Unchecked Exceptions, Error Propagation, Reliable Exception Design.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Java Level 4: Collections & Generics cover?
Lists, Sets and Maps, Generic Types, Choosing Appropriate Collections: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 3: Interfaces & Polymorphism, which covers Interfaces and Abstract Classes, Dynamic Dispatch, Designing Flexible Object Models.