-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumMapMethods.java
144 lines (101 loc) · 4.43 KB
/
EnumMapMethods.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.util.Collection;
import java.util.EnumMap;
import java.util.Map;
import java.util.Set;
public class EnumMapMethods {
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
public static void main(String[] args) {
EnumMap<Day, String> map = new EnumMap<>(Day.class);
// Put
map.put(Day.SUNDAY, "Sunday");
map.put(Day.MONDAY, "Monday");
map.put(Day.TUESDAY, "Tuesday");
map.put(Day.WEDNESDAY, "Wednesday");
map.put(Day.THURSDAY, "Thursday");
map.put(Day.FRIDAY, "Friday");
map.put(Day.SATURDAY, "Saturday");
System.out.println("Map:" + map);
// Clone
EnumMap<Day, String> map1 = new EnumMap<>(Day.class);
map1 = (EnumMap<Day, String>) map.clone();
System.out.println("Cloned Map:" + map1);
// containsKey
Boolean k = map.containsKey(Day.SUNDAY);
System.out.println("Is SUNDAY present in the map? " + k);
// containsValue
Boolean v = map.containsValue("Sunday");
System.out.println("Is Sunday present in the map? " + v);
// get
String g = map.get(Day.SUNDAY);
System.out.println("Value of SUNDAY is: " + g);
// getOrDefault
String gd = map.getOrDefault(Day.SUNDAY, "Sunday");
System.out.println("Value of SUNDAY is: " + gd);
// isEmpty
Boolean i = map.isEmpty();
System.out.println("Is the map empty? " + i);
// keySet
Set<Day> set = map.keySet();
System.out.println("Keys of the map are: " + set);
// size
int s = map.size();
System.out.println("Size of the map is: " + s);
// values
Collection<String> col = map.values();
System.out.println("Values of the map are: " + col);
// entrySet
Set<Map.Entry<Day, String>> set1 = map.entrySet();
System.out.println("Entries of the map are: " + set1);
// equals
Boolean e = map.equals(map1);
System.out.println("Is map equal to map1? " + e);
// hashCode
int h = map.hashCode();
System.out.println("Hashcode of the map is: " + h);
// toString
String t = map.toString();
System.out.println("String representation of the map is: " + t);
// clear
map.clear();
System.out.println("Map after clear:" + map);
// putAll
map.putAll(map1);
System.out.println("Map after putAll:" + map);
// remove
map.remove(Day.SUNDAY);
System.out.println("Map after remove:" + map);
// replace
map.replace(Day.SUNDAY, "Sunday");
System.out.println("Map after replace:" + map);
// replaceAll
map.replaceAll((k1, v1) -> "Sunday");
System.out.println("Map after replaceAll:" + map);
// merge
map.merge(Day.SATURDAY, "Sunday", (v2, v3) -> "Saturday");
System.out.println("Map after merge:" + map);
map.merge(Day.FRIDAY, "Sunday", (v2, v3) -> "Friday");
System.out.println("Map after merge:" + map);
// forEach
map.forEach((k1, v1) -> System.out.println("Key: " + k1 + " Value: " + v1));
// replace(K key, V oldValue, V newValue)
map.replace(Day.SATURDAY, "Saturday", "Sunday");
System.out.println("Map after replaceIfSame:" + map);
// putIfAbsent(K key, V value)
map.putIfAbsent(Day.SUNDAY, "Sunday");
System.out.println("Map after putIfAbsent:" + map);
// remove(K key, V value)
map.remove(Day.SUNDAY, "Sunday");
System.out.println("Map after remove:" + map);
// computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
map.computeIfAbsent(Day.SUNDAY, k1 -> "Sunday");
System.out.println("Map after computeIfAbsent:" + map);
// computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
map.computeIfPresent(Day.SUNDAY, (k1, v1) -> "Sunday");
System.out.println("Map after computeIfPresent:" + map);
// compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
map.compute(Day.SUNDAY, (k1, v1) -> "Sunday");
System.out.println("Map after compute:" + map);
}
}