Skip to content
Preview · sample content · Explore all pages

List, set or map?

Last updated: 28 Sept 20263 min read
guide
By AITrove Editorial

A collection contract describes the operations a caller needs; its implementation determines ordering and performance.

Select by behavior

  • List: an ordered sequence that can contain duplicates.
  • Set: membership with no duplicate elements under the implementation’s equality rules.
  • Map: values associated with keys.
  • Deque: insertions and removals at both ends.

Use ArrayList as a starting point for indexed sequence access. Consider ArrayDeque for endpoint work without null elements. Study LinkedList when cursor edits and the combined List/Deque contract matter.

A contract is narrower than an implementation

Declare List when callers need list operations, but choose the implementation from the actual workload. A contract-compatible substitution can still change performance: repeated indexed access that is cheap on an array-backed list is expensive on linked storage.

Map is not a Collection subtype. Its key set, value collection, and entry set expose views of the mappings with their own mutation rules. Decide whether the caller receives a live view or a copied snapshot.

Common Mistakes

Do not assume a Set preserves insertion order or a Map sorts keys. Those properties depend on the chosen implementation.

java-collections
Storage details