Skip to content

Commit 8f56fce

Browse files
Create HashMap.java
0 parents  commit 8f56fce

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

HashMap.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package hashing;
2+
3+
import java.util.*;
4+
class HashMap{
5+
public static void main(String args[]){
6+
HashMap<Integer,String> hm=new HashMap<Integer,String>();
7+
System.out.println("Initial list of elements: "+hm);
8+
hm.put(100,"Amit");
9+
hm.put(101,"Vijay");
10+
hm.put(102,"Rahul");
11+
12+
System.out.println("After invoking put() method ");
13+
for(Map.Entry m:hm.entrySet()){
14+
System.out.println(m.getKey()+" "+m.getValue());
15+
}
16+
17+
hm.putIfAbsent(103, "Gaurav");
18+
System.out.println("After invoking putIfAbsent() method ");
19+
for(Map.Entry m:hm.entrySet()){
20+
System.out.println(m.getKey()+" "+m.getValue());
21+
}
22+
HashMap<Integer,String> map=new HashMap<Integer,String>(); // another object map
23+
map.put(104,"Ravi");
24+
map.putAll(hm);
25+
System.out.println("After invoking putAll() method ");
26+
for(Map.Entry m:map.entrySet()){
27+
System.out.println(m.getKey()+" "+m.getValue());
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)