How do you build an LRU cache using LinkedHashMap?
Reviewed by Gurusankar M.
Short Answer
Construct LinkedHashMap with accessOrder=true (reorders on access) and override removeEldestEntry to evict when size exceeds capacity. It's O(1) get/put with automatic LRU eviction — but not thread-safe, so wrap or use Caffeine in production.
On this page
Concept Made Simple
Coffee Chat Question
“How do you build an LRU cache using LinkedHashMap?”
Remember It Faster
Mind Map Answer
accessOrder=true moves a touched entry to the tail, so the head is always the least-recently-used. removeEldestEntry returns true to evict it when over capacity.
Learn by Doing
Hands-on Keyboard
Map<K,V> lru = new LinkedHashMap<>(16, 0.75f, true) {
protected boolean removeEldestEntry(Map.Entry<K,V> e) {
return size() > MAX;
}
};- Time:
- O(1) get/put with LRU eviction
Think Beyond the Expected
What If?
Why wouldn't you ship this LinkedHashMap LRU to production as-is?
It isn't thread-safe and has no TTL, hit/miss metrics, or weak references. Under concurrency you'd wrap it (and synchronize iteration) or — better — use Caffeine/Guava which give bounded size, TTL, async loading and stats out of the box.
Real World
LinkedHashMap LRU is a great interview demonstration and fine for small single-threaded caches, but real services use Caffeine for bounded, concurrent, observable caching.
Interviewer's Expectation
Keywords they're listening for:
- accessOrder=true
- removeEldestEntry
- O(1) LRU
- not thread-safe
- Caffeine for prod
Common Mistakes
- Forgetting accessOrder=true (gives insertion-order, not LRU)
- Using it concurrently without synchronization
- Reinventing caching instead of using Caffeine
Best Practices
- Use Caffeine/Guava for production caches
- Bound every cache (size + TTL)
- Expose hit-rate metrics
Follow-up Questions
- How would you make it thread-safe?
- How does Caffeine's W-TinyLFU differ from pure LRU?
- How do you add TTL/expiry?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: HashMap Internals (Java Collections) Interview question: "How do you build an LRU cache using LinkedHashMap?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair