Commit 3dfa865
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- Section 25 Collections Frameworks/Map Interface/Linked HashMap/src
1 file changed
+59
-0
lines changedLines changed: 59 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
0 commit comments