Algorithms

LinkedHashMap

๐Ÿ“‹ Quick Reference

PropertyValue
TypeHashMap + doubly-linked list for ordering
Get/PutO(1) average
OrderingInsertion order (or access order)
MemoryO(n) + linked list overhead
NullOne null key, multiple null values
Best ForOrdered maps, LRU cache, predictable iteration
One-liner: HashMap with predictable iteration order - insertion order by default, or access order for LRU cache.

๐ŸŽฎ Interactive Visualizer

Watch how LinkedHashMap maintains order while providing O(1) operations:

Loading visualizer...
Try these operations:
  1. Put entries - see both hash bucket and linked list
  2. Iterate - entries come out in insertion order
  3. Remove and re-add - position changes
  4. Enable access order - see LRU behavior

๐Ÿ”ง Key Operations

Creating

JAVA(12 lines)
Code
Loading syntax highlighter...

Adding/Updating

JAVA(7 lines)
Code
Loading syntax highlighter...

Retrieving

JAVA(4 lines)
Code
Loading syntax highlighter...

Removing

JAVA(3 lines)
Code
Loading syntax highlighter...

Iteration (Ordered!)

JAVA(13 lines)
Code
Loading syntax highlighter...

๐Ÿ“Š Complexity Analysis

OperationTimeNotes
get(key)O(1)May reorder if access order
put(key, val)O(1)Updates linked list
remove(key)O(1)Updates linked list
containsKeyO(1)Hash lookup
containsValueO(n)Scans all values
iterationO(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)
Code
Loading syntax highlighter...

Manual LRU with Access Order

JAVA(6 lines)
Code
Loading syntax highlighter...

๐Ÿ”„ LinkedHashMap vs HashMap vs TreeMap

FeatureHashMapLinkedHashMapTreeMap
OrderNoneInsertion/AccessSorted
Get/PutO(1)O(1)O(log n)
MemoryLowestMediumHigher
Use CaseGeneralOrdered/LRUSorted keys
JAVA(4 lines)
Code
Loading syntax highlighter...

โš ๏ธ Common Pitfalls

1. Forgetting Access Order Moves Elements

JAVA(12 lines)
Code
Loading syntax highlighter...

2. Assuming Order After HashMap Conversion

JAVA(7 lines)
Code
Loading syntax highlighter...

3. Not Using for Predictable Tests

JAVA(6 lines)
Code
Loading syntax highlighter...

4. Missing LRU Behavior Configuration

JAVA(6 lines)
Code
Loading syntax highlighter...

๐ŸŽค Interview Tips

Q: How does LinkedHashMap maintain order?
"

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.

Q: How would you implement an LRU cache?
"

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.

Q: LinkedHashMap vs TreeMap for ordered iteration?
"

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".

Q: What's the memory overhead of LinkedHashMap?
"

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


Visualizer: LinkedHashMap from @tomaszjarosz/react-visualizers