Skip to content

Commit 3dfa865

Browse files
committed
feat(LinkedHashMapAccessOrderDemo): showcase LinkedHashMap with access-order enabled
What - Added LinkedHashMapAccessOrderDemo class. - Created a LinkedHashMap with parameters (initialCapacity=11, loadFactor=0.3f, accessOrder=true). - Inserted three entries: Orange, Apple, Guava. - Accessed entries with get() calls to trigger reordering. - Iterated and printed entries to demonstrate access-order iteration. Why - To explain how LinkedHashMap can operate in two modes: - Insertion order (default). - Access order (when accessOrder=true). - Demonstrates how entries are moved to the end on access, forming the foundation for LRU (Least Recently Used) cache implementations. How - Constructed LinkedHashMap with accessOrder flag set to true. - Called get() on "Apple", "Orange", and "Guava". - Iterated over entrySet() and printed results. - Added commentary showing how repeated access patterns shift entries. Logic - Inputs: key-value pairs of fruits and integers. - Processing: - On each get(key), LinkedHashMap internally re-links the accessed node to the tail of its doubly-linked list. - Iteration order now reflects most-recently accessed items last. - Outputs: - Final printed order corresponds to access order, not insertion order. Real-life applications - LinkedHashMap with accessOrder=true is widely used to build LRU caches. - Least-recently accessed entries remain at the front of the list. - Newest or most-recently accessed entries are moved to the end. - HashMap → no guaranteed order. - LinkedHashMap → guarantees either insertion or access order, making it more predictable in ordered-use cases. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 9ac62fc commit 3dfa865

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.util.LinkedHashMap;
2+
import java.util.Map;
3+
4+
public class LinkedHashMapAccessOrderDemo {
5+
public static void main(String[] args) {
6+
// LinkedHashMap with access-order enabled (true).
7+
// Constructor params → (initialCapacity=11, loadFactor=0.3f, accessOrder=true)
8+
// accessOrder = true → Entries are reordered whenever accessed (get/put).
9+
// threshold = capacity × loadFactor
10+
LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<>(11, 0.3f, true);
11+
12+
// Insertion of entries
13+
linkedHashMap.put("Orange", 10);
14+
linkedHashMap.put("Apple", 20);
15+
linkedHashMap.put("Guava", 13);
16+
17+
// Multiple get() calls reorder the entries in access order.
18+
// The most recently accessed entry always moves to the END.
19+
linkedHashMap.get("Apple");
20+
linkedHashMap.get("Orange");
21+
linkedHashMap.get("Guava");
22+
/*
23+
linkedHashMap.get("Apple");
24+
linkedHashMap.get("Orange");
25+
linkedHashMap.get("Apple");
26+
linkedHashMap.get("Guava");
27+
*/
28+
29+
// Iteration now respects *access order*.
30+
// That means the order you see here is not insertion order,
31+
// but the order in which elements were last accessed.
32+
System.out.println("LinkedHashMap (access order):");
33+
34+
for (Map.Entry<String, Integer> entry : linkedHashMap.entrySet()) {
35+
System.out.println(entry.getKey() + ": " + entry.getValue());
36+
}
37+
}
38+
}
39+
40+
/* LinkedHashMap = HashMap + Doubly Linked List + Configurable access order.
41+
• LinkedHashMap does guarantee ordering:
42+
• If accessOrder=false → preserves insertion order.
43+
• If accessOrder=true → preserves access order (most recently accessed entries go to the end).
44+
45+
Theory: LinkedHashMap with accessOrder = true
46+
47+
1. Insertion order is NOT preserved here. Instead, entries are reordered based on *access*.
48+
2. Each time you do get(key) or put(key, value):
49+
→ That entry moves to the END of the doubly-linked list.
50+
3. This is how LinkedHashMap can be used to implement LRU Cache:
51+
- The least recently used entries stay at the beginning.
52+
- The most recently accessed entries move to the end.
53+
54+
• HashMap → No ordering (entries are placed in buckets based on hash).
55+
56+
• LinkedHashMap → Ordering is guaranteed:
57+
• accessOrder=false → insertion order.
58+
• accessOrder=true → access order.
59+
*/

0 commit comments

Comments
 (0)