Skip to content

Commit c9f9e3d

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 89d2b06 commit c9f9e3d

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
package HashMapDemo.LinkedHashMapDemo;
2-
31
import java.util.HashMap;
42
import java.util.LinkedHashMap;
53
import java.util.Map;
64

7-
public class LinkedHashMapED {
5+
public class LinkedHashMapDemo1 {
86
public static void main(String[] args) {
97
LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<>();
10-
//But it maintain order.
11-
8+
// But it maintains order.
129
linkedHashMap.put("Orange",10);
1310
linkedHashMap.put("Apple",20);
1411
linkedHashMap.put("Guava",13);
15-
for(Map.Entry<String,Integer> entry : linkedHashMap.entrySet())
16-
{
12+
13+
for(Map.Entry<String,Integer> entry : linkedHashMap.entrySet()) {
1714
System.out.println(entry.getKey()+": "+entry.getValue());
1815
}
1916

@@ -23,16 +20,16 @@ public static void main(String[] args) {
2320
hashMap.put("Orange",10);
2421
hashMap.put("Apple",20);
2522
hashMap.put("Guava",13);
26-
//HashMap Doesn't maintain any order.
23+
// HashMap Doesn't maintain any order.
2724

28-
for(Map.Entry<String,Integer> entry : hashMap.entrySet())
29-
{
25+
for(Map.Entry<String,Integer> entry : hashMap.entrySet()) {
3026
System.out.println(entry.getKey()+": "+entry.getValue());
3127
}
3228
}
3329
}
3430

35-
//Key Takeaways
36-
//LinkedHashMap maintains insertion order.
37-
//HashMap does not maintain any order—it is based on hash codes.
38-
//The difference in ordering is clearly visible in the output.
31+
/* Key Takeaways:
32+
LinkedHashMap maintains insertion order.
33+
HashMap does not maintain any order it is based on hash codes.
34+
The difference in ordering is clearly visible in the output.
35+
*/

0 commit comments

Comments
 (0)