← Back to Week 3 Hub

ArrayList vs HashMap — Pick One

Two excellent collections, two different jobs. A quick decision guide, a comparison table, and 8 scenarios to test yourself.

ArrayList

An ordered sequence you access by position.

HashMap

A fast key → value lookup table.

Decision tree

Do you need to look up items by a unique identifier (like an id, SKU, username)?
YES
HashMap
Key = the identifier. get(key) returns the object.
NO
↓ next question
Does the order of items matter, or do you loop through all of them?
YES
ArrayList
Preserves insertion order, loops in sequence.
NO
ArrayList (default)
ArrayList is a fine default when lookup isn't needed.

Pick the right collection — 8 scenarios

Score: 0 / 0

Operation comparison

Need to…ArrayListHashMap
Add an itemlist.add(x)map.put(k, v)
Get by indexlist.get(i)not applicable
Get by keyloop + checkmap.get(k) — fast
Check if item existslist.contains(x)map.containsKey(k)
Removelist.remove(i)map.remove(k)
Size / countlist.size()map.size()
Loop through allfor (x : list)for (Map.Entry e : map.entrySet())
Preserves order?yes — insertion orderno — bucket order
Duplicates allowed?yeskeys no, values yes

Tip: when in doubt, ask "do I need to find this by a specific key?" If yes → HashMap. If no → ArrayList.

← HashMap Operations & Iteration Waterfall vs Agile →