-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main_HashMap.java
30 lines (25 loc) · 994 Bytes
/
Main_HashMap.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
import java.util.*;
public class Main_HashMap {
public static void main(String[] args) {
HashMap <String, Integer> hashMap = new HashMap<>();
//Adding keys to a hash map.
hashMap.put("John", 25);
hashMap.put("Alice", 30);
hashMap.put("Bob", 35);
//Displaying the integer values.
int alice = hashMap.get("Alice");
int john = hashMap.get("John");
System.out.println("Age of John: " + john);
System.err.println("Age of Alice: " + alice);
//Checking if a key is present.
Boolean isPresent = hashMap.containsKey("Bob");
System.out.println("Is Bob present? " + isPresent);
//Printing all key-value pairs.
for(String key: hashMap.keySet()){
System.out.println(key+":"+hashMap.get(key));
}
//Size of the hash map
int Size = hashMap.size();
System.out.println("Size of the hash map: "+Size);
}
}