Skip to content

Commit f4299ce

Browse files
committed
feat(LRUCacheDemo): implement simple LRU cache using LinkedHashMap
What - Added generic `LRUCacheDemo<K, V>` class extending LinkedHashMap. - Configured `accessOrder=true` to reorder entries based on usage. - Overrode `removeEldestEntry()` to evict least recently used element when capacity exceeded. - Added a demo in `main()` with a student marks example. Why - Demonstrates how LinkedHashMap can be leveraged to build an **LRU cache** with minimal code. - Useful for scenarios like caching results, database lookups, or frequently accessed data with limited memory. How - Constructor sets custom capacity and calls super with access-order enabled. - Each `put()` or `get()` operation moves the accessed key to the end of the order. - When size > capacity, the eldest (least recently used) entry is evicted automatically. - Demo: inserted 5 student entries with capacity = 3 to illustrate eviction. Logic 1. Insert "Bob", "Alice", "Ram" → cache full: ["Bob", "Alice", "Ram"] 2. Update "Bob" → reorders to most recent: ["Alice", "Ram", "Bob"] 3. Insert "Vipul" → capacity exceeded → removes eldest ("Alice"): ["Ram", "Bob", "Vipul"] Real-life applications - Browser history or session caching. - Database query result cache. - Image/object caching in web apps or mobile apps. - Any system where **least recently used** data should be automatically discarded. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent e1813d5 commit f4299ce

File tree

1 file changed

+6
-7
lines changed
  • Section 25 Collections Frameworks/Map Interface/Linked HashMap/LRU Cache/src

1 file changed

+6
-7
lines changed

Section25CollectionFramework/src/HashMapDemo/LinkedHashMapDemo/LRUCache.java renamed to Section 25 Collections Frameworks/Map Interface/Linked HashMap/LRU Cache/src/LRUCacheDemo.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
package HashMapDemo.LinkedHashMapDemo;
2-
31
import java.util.LinkedHashMap;
42
import java.util.Map;
53

6-
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
4+
public class LRUCacheDemo<K, V> extends LinkedHashMap<K, V> {
75
private int capacity;
86

9-
public LRUCache(int capacity) {
7+
public LRUCacheDemo(int capacity) {
108
super(capacity, 0.75f, true);
119
this.capacity = capacity;
1210
}
@@ -17,7 +15,8 @@ protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
1715
}
1816

1917
public static void main(String[] args) {
20-
LRUCache<String, Integer> studentMap = new LRUCache<>(3);
18+
LRUCacheDemo<String, Integer> studentMap = new LRUCacheDemo<>(3);
19+
2120
studentMap.put("Bob", 99);
2221
studentMap.put("Alice", 89);
2322
studentMap.put("Ram", 91);
@@ -27,8 +26,8 @@ public static void main(String[] args) {
2726
System.out.println(studentMap);
2827
}
2928
}
30-
/*
31-
the most recently accessed element moves to the end. This method ensures that the eldest entry (least recently used)
29+
30+
/* the most recently accessed element moves to the end. This method ensures that the eldest entry (least recently used)
3231
is removed once the cache exceeds the given capacity.
3332
3433
studentMap.put("Bob", 99); ["Bob"]

0 commit comments

Comments
 (0)