-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapmethods9.java
59 lines (42 loc) · 1.58 KB
/
mapmethods9.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
import java.util.HashMap;
public class mapmethods9 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
map.put("Four", 4);
map.put("Five", 5);
map.put("Six", 6);
System.out.println("Before clear:" + map);
map.forEach((String key, Integer value) -> {
System.out.println(key + ":" + value);
});
System.out.println(map.keySet());
//keySet().forEach()
map.keySet().forEach((String key) -> {
System.out.println(key);
});
// keySet().size()
System.out.println(map.keySet().size());
// keySet().toArray()
Object[] arr = map.keySet().toArray();
for (Object obj : arr) {
System.out.println(obj);
}
// keySet().removeIf()
map.keySet().removeIf((String key) -> {
return key.equals("One");
});
System.out.println("After removeIf:" + map);
// keySet().remove()
map.keySet().remove("Two");
System.out.println("After remove:" + map);
// keySet().retainAll()
map.keySet().retainAll(map.keySet());
System.out.println("After retainAll:" + map);
// keySet().removeAll()
map.keySet().removeAll(map.keySet());
System.out.println("After removeAll:" + map);
}
}