Skip to content

Commit caa4482

Browse files
committed
feat(LinkedHashMapDemo3): demonstrate size-limited LinkedHashMap with removeEldestEntry
What - Added LinkedHashMapDemo3 class. - Created an anonymous subclass of LinkedHashMap overriding `removeEldestEntry`. - Enforced automatic eviction of the eldest entry when map size exceeds 7. - Inserted multiple entries and accessed some keys to show access-order behavior. - Printed final contents to illustrate automatic removal. Why - To demonstrate how LinkedHashMap can be customized to function as a bounded cache (like LRU cache). - Shows the real-world use case of `removeEldestEntry()` for controlling map size. - Useful for scenarios where memory needs to be managed automatically (e.g., caching recently used items). How - Constructed LinkedHashMap with `accessOrder=true` to maintain order based on recent access. - Overrode `removeEldestEntry` to return true when `size() > 7`, ensuring eviction policy. - Added 8 entries, with key `8` triggering removal of the least recently accessed element. - Accessed keys `2`, `5`, and `1` before inserting `8` to reorder usage. - Iterated with forEach to display results. Logic - Input: put 8 entries into a LinkedHashMap with max size 7. - Processing: `removeEldestEntry` checks if size > 7 → removes eldest entry automatically. - Because `accessOrder=true`, "eldest" is defined by least recent access, not oldest insertion. - Output: final map contains 7 entries (key 8 present, one least accessed entry removed). Real-life applications - Implementing **LRU (Least Recently Used) caches** in memory-sensitive apps. - Bounded-size maps where stale entries should be discarded automatically. - Frequently used in frameworks like Spring, Hibernate, or custom cache implementations. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent b28fa8e commit caa4482

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
1-
package HashMapDemo.LinkedHashMapDemo;
2-
31
import java.util.LinkedHashMap;
42
import java.util.Map;
53

6-
public class LinkedHashMapFixSize
7-
{
8-
public static void main(String[] args)
9-
{
4+
public class LinkedHashMapDemo3 {
5+
public static void main(String[] args) {
106
// Create an anonymous subclass of LinkedHashMap that overrides removeEldestEntry
11-
LinkedHashMap<Integer, String> LHM = new LinkedHashMap<>(5, 0.75F, true)
12-
{
7+
LinkedHashMap<Integer, String> LHM = new LinkedHashMap<>(5, 0.75F, true) {
138
@Override
14-
protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest)
15-
{
9+
protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest) {
1610
// Remove the eldest entry if the size exceeds 7
1711
return size() > 7;
1812
}
1913
};
2014

21-
//Suppose you want to limit the size of the Linked-Hash Map then you need to override the method
22-
//inside Class and anonymous class must be...protected boolean ....
15+
// Suppose you want to limit the size of the Linked-Hash Map then you need to override the method
16+
// inside Class and anonymous class must be...protected boolean ....
2317

2418
LHM.put(1,"Hello");
2519
LHM.put(2,"Bye");
@@ -30,9 +24,10 @@ protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest)
3024
LHM.put(7,"Just Grind Hard Everyday and night");
3125

3226
String s = LHM.get(2);
27+
3328
s = LHM.get(5);
3429
s= LHM.get(1);
3530
LHM.put(8,"Least access element removed and here this is the new element added");
3631
LHM.forEach((k,v)->System.out.println(k+" "+v));
3732
}
38-
}
33+
}

0 commit comments

Comments
 (0)