-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashMapfunctions.java
52 lines (43 loc) · 1.94 KB
/
hashMapfunctions.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.HashMap;
import java.util.Map;
public class hashMapfunctions {
public static void main(String[] args) {
HashMap<String, Integer> mp = new HashMap<>();
// to add key value pair in hashmap
mp.put("Rahul", 30025);
mp.put("Aman", 3007);
mp.put("Amitosh", 3008);
mp.put("Anand", 3010);
// to retrieve value from hashmap
System.out.println(mp.get("Rahul"));// -> 30025
// to update value in existing value in hashmap
mp.put("Rahul", 3125);
System.out.println(mp.get("Rahul")); // updated value -> 3125
// TO REMOVE A VALUE FROM HASHMAP
System.out.println(mp.remove("Amitosh"));// return the value after deletion
// TO CHECK WETHER THE KEY IS PRESENT IN HASHMAP OR NOT
System.out.println(mp.containsKey("Amitosh")); // return Boolean value
// TO ADD TO HASHMAP IF NOT PRESENT IN HASHMAP
mp.putIfAbsent("Anand Pathak", 30010);
// GET ALL KEY OF HASHMAP
System.out.println(mp.keySet());
// GET ALL ENTRY (VALUE) OF HASH MAP
System.out.println(mp.entrySet());
// TRAVERSING THE HASHMAP
/* 1 FOR EACH */
System.out.println("1st approach using for each in for loop");
for (String key : mp.keySet()) {
System.out.printf("Roll number of %s is %d \n", key, mp.get(key));
}
System.out.println("2nd approach using Map.Enttry datatype");
/* 2 using Map.Entry<key,value> datatype */
for (Map.Entry<String, Integer> e : mp.entrySet()) {
System.out.printf("Roll number of %s is %d \n", e.getKey(), e.getValue());
}
System.out.println("3rd approach using var keyboard");
/* 3 USING VAR keyword */
for (var e : mp.entrySet()) {
System.out.printf("Roll number of %s is %d \n", e.getKey(), e.getValue());
}
}
}