Choosing the right collection

Collections · lesson 16 of 42 · 3 min read

Answer four questions and land on the correct collection every time.

Open this lesson in the learning hub

Key points

  • Key-value data? Use a Map. Otherwise a Collection. That is the first fork.
  • Duplicates allowed and position matters? ArrayList. Duplicates not allowed? A Set.
  • Then pick the order guarantee: none = HashMap, insertion = LinkedHashMap, sorted = TreeMap.
  • Both ends? ArrayDeque. Smallest out first? PriorityQueue. Enum keys? EnumMap.
  • Shared across threads? Take the concurrent version. Never shared and never changed? List.of.
  • Honest default: ArrayList and HashMap handle the vast majority of code. Change only when a requirement forces you.

Duplicates, order, key lookup, threading. Four questions, one right answer.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Collections course, and every lesson in it is listed on the Collections contents page.