Choosing the right collection
Answer four questions and land on the correct collection every time.
Open this lesson in the learning hubKey points
- Key-value data? Use a
Map. Otherwise aCollection. That is the first fork. - Duplicates allowed and position matters?
ArrayList. Duplicates not allowed? ASet. - 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:
ArrayListandHashMaphandle 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.