How do HashSet/HashMap, LinkedHashSet/LinkedHashMap and TreeSet/TreeMap differ, and which do you pick?
Short answer
Hash*: O(1), no order. Linked*: like Hash but keeps insertion order. Tree*: O(log n), sorted by key, supports range queries.
3 points to say:
- Default to
HashMap,HashSet. - Need insertion order:
LinkedHashMap. - Need sorting or range queries:
TreeMap.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.