-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_hash_map.java
More file actions
49 lines (36 loc) · 1.38 KB
/
03_hash_map.java
File metadata and controls
49 lines (36 loc) · 1.38 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
import java.util.*;
class MyCollections {
public static void main(String args[]) {
example_hashMap();
}
public static void example_hashMap() {
// Create using
// HashMap <Key_dataType, Value_dataType> variable = new HashMap <Key_dataType, Value_dataType> ();
HashMap <String, String> workingDaysMap = new HashMap <String, String> ();
workingDaysMap.put("Day-1", "Monday");
workingDaysMap.put("Day-2", "Tuesday");
workingDaysMap.put("Day-3", "Wednesday");
workingDaysMap.put("Day-4", "Thursday");
workingDaysMap.put("Day-5", "Friday");
// Size of the map
System.out.println("\nWorkday Map Size : " + workingDaysMap.size() );
// Get value of key using get function
System.out.println("\nWeekday #2 is " + workingDaysMap.get("Day-2") );
workingDaysMap.putIfAbsent("Day-3", "Saturday");
// Existence
if( workingDaysMap.containsKey("Day-3") == true) {
System.out.println("\nDay-3 value is " + workingDaysMap.get("Day-2") );
} else {
System.out.println("\nDay-3 doesn't exist" );
}
// iterating
// All values
System.out.println( "\nAll Values :" );
Collection valueCollection = workingDaysMap.values();
System.out.println( valueCollection );
// All Keys
System.out.println( "\nAll Keys :" );
Collection keys = workingDaysMap.keySet();
System.out.println( keys );
}
}