LRU -MRU Cache Implementation
Two Ways to Implement LRU Cache 1️⃣ Easy Way (using LinkedHashMap ) In the code we just wrote with LinkedHashMap , we don’t manually write a doubly linked list . That’s because LinkedHashMap internally already uses a doubly linked list to maintain order (either insertion order or access order). So when we write:new LinkedHashMap<>(capacity, 0.75f, true) That true tells it to maintain the doubly linked list in access order . We don’t see it in code because it’s hidden inside the JDK implementation . 👉 This is why the LinkedHashMap solution is the easy shortcut 2️⃣ Full Custom Way (HashMap + Doubly Linked List) In FAANG-style interviews, sometimes they say: “Don’t use LinkedHashMap, implement it yourself.” In that case, you must build your own doubly linked list : HashMap<key, Node> → fast lookup Node → custom class with key, value, prev, next head → most recentl...