-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap_00.java
More file actions
57 lines (35 loc) · 1.11 KB
/
Copy pathHashMap_00.java
File metadata and controls
57 lines (35 loc) · 1.11 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
import java.util.*;
class HashMap_00 {
public static void main(String args[]) {
//Creation
HashMap<String, Integer> map = new HashMap<>();
//Insertion
map.put("India", 120);
map.put("US", 30);
map.put("China", 150);
System.out.println(map);
map.put("China", 180);
System.out.println(map);
//Searching
if(map.containsKey("Indonesia")) {
System.out.println("key is present in the map");
} else {
System.out.println("key is not present in the map");
}
System.out.println(map.get("China")); //key exists
System.out.println(map.get("Indonesia")); //key doesn't exist
//Iteration (1)
for( Map.Entry<String, Integer> e : map.entrySet()) {
System.out.println(e.getKey());
System.out.println(e.getValue());
}
//Iteration (2)
Set<String> keys = map.keySet();
for(String key : keys) {
System.out.println(key+ " " + map.get(key));
}
//Removing
map.remove("China");
System.out.println(map);
}
}