Two excellent collections, two different jobs. A quick decision guide, a comparison table, and 8 scenarios to test yourself.
get(0), get(1), …put overwrites)| Need to… | ArrayList | HashMap |
|---|---|---|
| Add an item | list.add(x) | map.put(k, v) |
| Get by index | list.get(i) | not applicable |
| Get by key | loop + check | map.get(k) — fast |
| Check if item exists | list.contains(x) | map.containsKey(k) |
| Remove | list.remove(i) | map.remove(k) |
| Size / count | list.size() | map.size() |
| Loop through all | for (x : list) | for (Map.Entry e : map.entrySet()) |
| Preserves order? | yes — insertion order | no — bucket order |
| Duplicates allowed? | yes | keys no, values yes |
Tip: when in doubt, ask "do I need to find this by a specific key?" If yes → HashMap. If no → ArrayList.