Module 11: Collections Framework in Practice
Maps for Fast Lookup
Store and retrieve values by key with clear and predictable map code.
Author
Java Learner Editorial Team
Reviewer
Technical review by Java Learner
Last reviewed
2026-04-17
Java version
Java 25 LTS
Learning goals
- Use maps when lookup by key is the real task
- Choose between `HashMap`, `LinkedHashMap`, and `TreeMap`
- Write clearer map access code
Maps solve a different problem than lists or sets: They answer “given this key, what value belongs to it?”
HashMap is the default choice for fast key lookup: It does not guarantee iteration order.
LinkedHashMap preserves insertion order, and TreeMap keeps keys sorted: Use those only when you need that behavior.
Good map code uses meaningful keys: IDs, usernames, codes, or dates make maps easier to reason about than vague indexes.
Runnable examples
Use keys to retrieve values directly
java.util.Map<String, Integer> scores = new java.util.HashMap<>();
scores.put("Ada", 95);
scores.put("Grace", 99);
System.out.println(scores.get("Grace"));Expected output
99
Mini exercise
Create a map from product name to price and print one product’s price by key.
Summary
- Maps are for key-value lookup.
- `HashMap` is the general-purpose default.
- Choose ordered map types only when the order is part of the requirement.
Next step
Finish the collections toolbox with ordering strategies and a practical mini-project.
Sources used