The app for independent voices

How fast are common Java data structures (10M operations)?

HashMap vs TreeMap vs ArrayList vs LinkedList

HashMap.get() → ~140 ms

TreeMap.get() → ~420 ms

ArrayList.get(i) → ~40 ms

LinkedList.get(i) → ~2.5 s

Insertion (10M elements):

ArrayList.add() → ~180 ms

HashMap.put() → ~300 ms

LinkedList.add() → ~900 ms

Why:

ArrayList = contiguous memory, cache-friendly

HashMap = hashing + pointer chasing

TreeMap = O(log n) + rebalancing

LinkedList = worst cache locality + pointer traversal

PS: These are baseline costs. Once you know this, you stop misusing LinkedList forever.

Dec 23
at
3:30 AM

Log in or sign up

Join the most interesting and insightful discussions.