forked from VarshaDas/Java-Code-Snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedHashMapDemo.java
More file actions
35 lines (28 loc) · 1.29 KB
/
LinkedHashMapDemo.java
File metadata and controls
35 lines (28 loc) · 1.29 KB
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
package main.java;
import java.util.LinkedHashMap;
import java.util.Map;
public class LinkedHashMapDemo {
public static void main(String[] args) {
// Create a LinkedHashMap with access order set to true
Map<String, Integer> linkedHashMap = new LinkedHashMap<>(16, 0.75f, true);
//Map<String, Integer> linkedHashMap = new LinkedHashMap<>(16, 0.75f, false);
// Add elements to the LinkedHashMap
linkedHashMap.put("One", 1);
linkedHashMap.put("Two", 2);
linkedHashMap.put("Three", 3);
linkedHashMap.put("Four", 4);
// Print the elements in insertion order
System.out.println("LinkedHashMap elements in insertion order:");
printMap(linkedHashMap);
// Access an element to change the access order
System.out.println("\nAccessing an element to change access order: "+linkedHashMap.get("Two"));
// Print the elements again to show access order
System.out.println("\nLinkedHashMap elements after accessing an element (access order):");
printMap(linkedHashMap);
}
private static void printMap(Map<String, Integer> map) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}