-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaps.java
More file actions
31 lines (23 loc) · 860 Bytes
/
Maps.java
File metadata and controls
31 lines (23 loc) · 860 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
import java.util.*;
public class Maps
{
public static void main(String[] args)
{
HashMap<String, Integer> empIds = new HashMap<>();
empIds.put("John", 1234);
empIds.put("Carl", 2345);
empIds.put("Jerry", 7859);
System.out.println(empIds);
System.out.println(empIds.containsKey("Carl"));
System.out.println(empIds.containsValue(7859));
empIds.replace("John", 7890);
System.out.println(empIds.get("John"));
empIds.replace("Kramer", 8999);
//put vs replace - put will create a field if that key doesn't exist
//whereas if we use replace for a key that doesn't exist, our mapping won't be impacted
empIds.putIfAbsent("John", 222);
empIds.putIfAbsent("Kim", 435);
empIds.remove("Kim");
System.out.println(empIds);
}
}