-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhashmapput.java
More file actions
54 lines (48 loc) · 1.99 KB
/
Copy pathhashmapput.java
File metadata and controls
54 lines (48 loc) · 1.99 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
50
51
52
53
54
import java.util.*;
/**
* hashmapput
*/
public class hashmapput {
public static void main(String[] args) {
//Create an empty hash map by declaring object
// of string and integer type....
Map<String,Integer> mp = new HashMap<>();
// adding elements to the Map
mp.put("Ram", 100);
mp.put("Krishna", 150);
System.out.println();
System.out.println("After Adding Elements");
System.out.println();
//Printing element
System.out.println(mp.get("Ram"));
System.out.println(mp.get("Krishna"));
System.out.println();
//Printing all the element in the Hashmap using for each loop...
for (Map.Entry m : mp.entrySet())
System.out.println("Key: " +m.getKey()+" Value:"+m.getValue());
int key=15;//We are searching this Key in our Hasmap..
if(mp.containsKey(key))
{
Integer val=(Integer) mp.get(key);
System.out.println("\nValue Corresponding to the given Key is "+val);
}
else{ System.out.println("The Given Key does not Exist!");}
//Putting a New value against existing Key
mp.put("Krishna",200);
System.out.println("\n\nAfter Updating Value Against Same Key");
for (Map.Entry m : mp.entrySet())
System.out.println("Key: " +m.getKey()+" Value:"+m.getValue());
//Removing the pair whose Key is Krishna
mp.remove("Krishna");
System.out.println("\n\nAfter Removing One Pair from the Hashmap");
for (Map.Entry m1 : mp.entrySet())
System.out.println("Key: " +m1.getKey()+" Value:"+m1.getValue());
//using of ifabsent it will add the entries only if the enteries is not added or not found.
mp.putIfAbsent("Balram",300);
mp.putIfAbsent("Balram",400);
System.out.println("\n\nUsing putifAbsent Method");
for (Map.Entry m2 : mp.entrySet()) //m2 is a variable (use as itrater )
System.out.println("Key: " +m2.getKey()+" Value:"
+"\""+m2.getValue()+"\"");
}
}