SteelReed

Search

Search posts, questions…

Collections & DSAQuestion 5 / 25

Junior

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.

NoneInsertion orderSorted by key, rangeNeed a Map or SetWhat order?HashMap / HashSetO(1) averageLinkedHashMapLinkedHashSetTreeMap / TreeSetO(log n)

3 points to say:

  1. Default to HashMap, HashSet.
  2. Need insertion order: LinkedHashMap.
  3. 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.