forked from akash-coded/core-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapDemo.java
More file actions
60 lines (51 loc) · 1.8 KB
/
Copy pathMapDemo.java
File metadata and controls
60 lines (51 loc) · 1.8 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
53
54
55
56
57
58
59
60
package src.collections.maps;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
public class MapDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("One", "1");
map.put("Five", "5");
map.put("Four", "4");
map.put("Two", "2");
map.put("Three", "3");
System.out.println("HashMap: \n" + map);
System.out.println();
Map<String, String> map1 = new LinkedHashMap<>();
map1.put("One", "1");
map1.put("Five", "5");
map1.put("Four", "4");
map1.put("Two", "2");
map1.put("Three", "3");
System.out.println("LinkedHashMap: \n" + map1);
System.out.println();
Map<String, String> map2 = new TreeMap<>();
map2.put("One", "1");
map2.put("Five", "5");
map2.put("Four", "4");
map2.put("Two", "2");
map2.put("Three", "3");
System.out.println("TreeMap: \n" + map2);
System.out.println();
System.out.println("Set of keys::");
for (String key : map2.keySet()) {
System.out.println(key);
}
System.out.println();
System.out.println("List of values::");
for (String value : map2.values()) {
System.out.println(value);
}
System.out.println();
System.out.println("Set of map entries::");
for (Map.Entry<String, String> entry : map2.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
System.out.println();
System.out.println("Using forEach() method to iterate over a map::");
map2.forEach((key, value) -> System.out.println(key + " : " + value));
System.out.println();
}
}