-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestHashMap.java
More file actions
35 lines (27 loc) · 818 Bytes
/
TestHashMap.java
File metadata and controls
35 lines (27 loc) · 818 Bytes
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
import java.util.*;
/*
* Remember
* containsKey is O(1)
* containsValue is O(n)
* Iterating over hashmap
*/
public class TestHashMap {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("a", 100);
map.put("b", 200);
map.put("c", 300);
//get key
System.out.println("key for b is " + map.getOrDefault("b", 0));
System.out.println("key for z is " + map.getOrDefault("z", 0));
if (map.containsKey("a")) {
System.out.println("a exist as key");
}
if (map.containsValue(100)) {
System.out.println("100 exist as value");
}
map.forEach ( (key, value) -> {
System.out.println("key is " + key + " value is " + value);
});
}
}