-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapmethods17.java
90 lines (81 loc) · 2.67 KB
/
mapmethods17.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
import java.util.HashMap;
import java.util.Iterator;
public class mapmethods17 {
public static void main(String[] args) throws Exception {
HashMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("d", 4);
map.put("e", 5);
map.put("f", 6);
System.out.println("Map:" + map);
System.out.println(" ");
map.values().forEach(System.out::println);
System.out.println(" ");
map.values().forEach((v) -> System.out.println(v));
System.out.println(" ");
System.out.println("Values :" + map.values());
// remove
map.values().remove(2);
System.out.println(" ");
System.out.println("Map:" + map);
System.out.println("Map:" + map.values());
System.out.println(" ");
// removeAll
map.values().removeAll(map.values());
System.out.println(map.values());
System.out.println(" ");
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("d", 4);
map.put("e", 5);
map.put("f", 6);
// removeIf
map.values().removeIf((v) -> v % 2 == 0);
System.out.println(" ");
System.out.println("Map:" + map);
System.out.println(" ");
//retainAll
HashMap<String, Integer> map1 = new HashMap<>();
map1.put("a", 3);
map1.put("b", 4);
map1.put("c", 5);
map1.put("d", 2);
System.out.println("Map1: " + map1);
System.out.println("Map1: "+ map1.values());
map.values().retainAll(map1.values());
System.out.println(" ");
System.out.println("Map:" + map);
System.out.println(" ");
System.out.println(map.values());
//Clear
map.values().clear();
System.out.println("Map:"+ map);
//Iterator
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("d", 4);
map.put("e", 5);
map.put("f", 6);
System.out.println(" ");
System.out.println("Map:" + map);
System.out.println(" ");
Iterator<Integer> itr = map.values().iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
System.out.println(" ");
//Iterator remove
itr = map.values().iterator();
while(itr.hasNext()){
itr.next();
itr.remove();
}
System.out.println(" ");
System.out.println("Map:" + map);
System.out.println(" ");
}
}