Skip to content

Commit 89d2b06

Browse files
committed
feat(LinkedHashMapDemo): demonstrate insertion, access order, and iteration in LinkedHashMap
What - Added LinkedHashMapDemo class. - Constructed a LinkedHashMap with parameters `(5, 0.75F, true)`: - initial capacity = 5 - load factor = 0.75 - accessOrder = true (entries reorder on access). - Inserted seven key-value pairs with integer keys and string values. - Accessed keys 2, 5, and 1 using get(). - Iterated with forEach() to print the order. Why - To show how LinkedHashMap behaves with access-order enabled. - Demonstrates that most recently accessed entries are moved to the end of the iteration order. - Helps explain its relevance for caching strategies like LRU. How - Populated LHM with multiple entries. - Called get(2), get(5), and get(1) to simulate access. - Iterated with LHM.forEach((k,v) -> …) to display access-order iteration. Logic - Inputs: - Integer keys 1–7 with string messages as values. - Processing: - LinkedHashMap maintains a doubly-linked list of entries. - With accessOrder=true, each access (get/put) moves the entry to the tail. - Outputs: - Printed iteration order shows least recently accessed entries first, most recently accessed last. Real-life applications - LinkedHashMap with access-order is a foundation for implementing LRU caches: - Override `removeEldestEntry()` to automatically evict oldest entries when a size limit is exceeded. - Useful in memory-sensitive apps, caching layers, and session management. - Compared: - HashMap → no order guarantee. - LinkedHashMap (default) → preserves insertion order. - LinkedHashMap (accessOrder=true) → preserves access order. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 3dfa865 commit 89d2b06

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
package HashMapDemo.LinkedHashMapDemo;
2-
31
import java.util.LinkedHashMap;
42

5-
public class LinkedHashMapBasics {
3+
public class LinkedHashMapDemo {
64
public static void main(String[] args) {
7-
/*LinkedHashMap<Integer, String> LHM = new LinkedHashMap<>(5);*/
5+
/* LinkedHashMap<Integer, String> LHM = new LinkedHashMap<>(5); */
86

97
LinkedHashMap<Integer, String> LHM = new LinkedHashMap<>(5,.75F,true);
10-
//true = means order of their access . 1st key show most recently used key.
11-
//You can change the loading factor. 0.75...
8+
// true = means order of their access . 1st key show most recently used key.
9+
// You can change the loading factor. 0.75...
1210

1311
LHM.put(1,"Hello");
1412
LHM.put(2,"Bye");
@@ -18,17 +16,17 @@ public static void main(String[] args) {
1816
LHM.put(6,"I am the BatMan");
1917
LHM.put(7,"Just Grind Hard Everyday and night");
2018

21-
//Take String references for value.
19+
// Take String references for value.
2220
String s = LHM.get(2);
2321
String s1 = LHM.get(5);
2422
String s2= LHM.get(1);
25-
//Keys ko access kar Rahe hai and Most recently access key and Most recently accessed key.
23+
// Keys ko access kar Rahe hai and Most recently access key and Most recently accessed key.
2624
//Most recently accessed at the last and least recently accessed at the start.
2725

2826
LHM.forEach((k,v)->System.out.println(k+" "+v));
2927
}
3028
}
31-
/*
32-
Suppose you want to limit the size of the Linked-Hash Map then you need to override the method inside Class
33-
and anonymous class
34-
must be...protected boolean ....*/
29+
30+
/* Suppose you want to limit the size of the Linked-Hash Map then you need to override the method inside Class
31+
and anonymous class must be...protected boolean ....
32+
*/

0 commit comments

Comments
 (0)