LinkedHashMap
๐ Quick Reference
| Property | Value |
|---|---|
| Type | HashMap + doubly-linked list for ordering |
| Get/Put | O(1) average |
| Ordering | Insertion order (or access order) |
| Memory | O(n) + linked list overhead |
| Null | One null key, multiple null values |
| Best For | Ordered maps, LRU cache, predictable iteration |
๐ฎ Interactive Visualizer
Watch how LinkedHashMap maintains order while providing O(1) operations:
Loading visualizer...
- Put entries - see both hash bucket and linked list
- Iterate - entries come out in insertion order
- Remove and re-add - position changes
- Enable access order - see LRU behavior
๐ง Key Operations
Creating
JAVA(12 lines)CodeLoading syntax highlighter...
Adding/Updating
JAVA(7 lines)CodeLoading syntax highlighter...
Retrieving
JAVA(4 lines)CodeLoading syntax highlighter...
Removing
JAVA(3 lines)CodeLoading syntax highlighter...
Iteration (Ordered!)
JAVA(13 lines)CodeLoading syntax highlighter...
๐ Complexity Analysis
| Operation | Time | Notes |
|---|---|---|
get(key) | O(1) | May reorder if access order |
put(key, val) | O(1) | Updates linked list |
remove(key) | O(1) | Updates linked list |
containsKey | O(1) | Hash lookup |
containsValue | O(n) | Scans all values |
iteration | O(n) | Follows linked list |
Internal Structure
Hash Buckets: Doubly-Linked List: [0] โ null head โ โ A โ โ B โ โ C โ โ tail [1] โ B โ โ [2] โ A โ C first last inserted [3] โ null Each entry has: hash, key, value, next (bucket), before, after (linked list)
โ When to Use LinkedHashMap
Good Use Cases
- Predictable iteration - need consistent order
- LRU cache - access-order mode with removeEldestEntry
- JSON serialization - preserve field order
- Duplicate-preserving - remember first occurrence position
- Copy with order - when HashMap order is chaotic
Avoid When
- Order doesn't matter - use HashMap (less overhead)
- Need sorted order - use TreeMap
- Memory constrained - extra pointers per entry
- Thread safety needed - no concurrent version exists
๐งฉ LRU Cache Implementation
JAVA(23 lines)CodeLoading syntax highlighter...
Manual LRU with Access Order
JAVA(6 lines)CodeLoading syntax highlighter...
๐ LinkedHashMap vs HashMap vs TreeMap
| Feature | HashMap | LinkedHashMap | TreeMap |
|---|---|---|---|
| Order | None | Insertion/Access | Sorted |
| Get/Put | O(1) | O(1) | O(log n) |
| Memory | Lowest | Medium | Higher |
| Use Case | General | Ordered/LRU | Sorted keys |
JAVA(4 lines)CodeLoading syntax highlighter...
โ ๏ธ Common Pitfalls
1. Forgetting Access Order Moves Elements
JAVA(12 lines)CodeLoading syntax highlighter...
2. Assuming Order After HashMap Conversion
JAVA(7 lines)CodeLoading syntax highlighter...
3. Not Using for Predictable Tests
JAVA(6 lines)CodeLoading syntax highlighter...
4. Missing LRU Behavior Configuration
JAVA(6 lines)CodeLoading syntax highlighter...
๐ค Interview Tips
"It extends HashMap and adds a doubly-linked list through all entries. Each entry has before/after pointers in addition to the hash bucket chain. Iteration follows the linked list, not the buckets.
"Use LinkedHashMap with access-order=true and override removeEldestEntry() to return true when size exceeds capacity. Gets and puts move entries to the tail; eviction removes from head.
"LinkedHashMap maintains insertion (or access) order with O(1) operations. TreeMap maintains sorted order with O(log n) operations. Choose based on whether you need "order added" or "sorted by key".
"Each entry has two extra references (before/after) for the linked list, plus the head/tail references. This is typically 16-24 bytes per entry overhead compared to HashMap.
๐ Series Navigation
LinkedHashMap from @tomaszjarosz/react-visualizers