-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMapsDemo.java
More file actions
52 lines (32 loc) · 1.27 KB
/
MapsDemo.java
File metadata and controls
52 lines (32 loc) · 1.27 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package Maps;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
public class MapsDemo {
public static void run() {
System.out.println("=== Maps Demo ===");
Map<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "One");
hashMap.put(2, "Two");
hashMap.put(3, "Three");
System.out.println("HashMap: " + hashMap);
Map<String, Integer> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("Apples", 5);
linkedHashMap.put("Bananas", 3);
linkedHashMap.put("Oranges", 8);
System.out.println("LinkedHashMap: " + linkedHashMap);
Map<Integer, String> treeMap = new TreeMap<>();
treeMap.put(30, "C");
treeMap.put(10, "A");
treeMap.put(20, "B");
System.out.println("TreeMap: " + treeMap);
System.out.println("hashMap.get(2): " + hashMap.get(2));
System.out.println("hashMap.containsKey(3): " + hashMap.containsKey(3));
System.out.println("Loop through HashMap:");
for (Map.Entry<Integer, String> entry : hashMap.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
System.out.println();
}
}