Skip to content

Commit c5be7d7

Browse files
authored
Hashmap in JAVA
1 parent 5d925c5 commit c5be7d7

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Java program to illustrate
2+
// Java.util.HashMap
3+
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class GFG {
8+
public static void main(String[] args)
9+
{
10+
11+
HashMap<String, Integer> map
12+
= new HashMap<>();
13+
14+
print(map);
15+
map.put("vishal", 10);
16+
map.put("sachin", 30);
17+
map.put("vaibhav", 20);
18+
19+
System.out.println("Size of map is:- "
20+
+ map.size());
21+
22+
print(map);
23+
if (map.containsKey("vishal")) {
24+
Integer a = map.get("vishal");
25+
System.out.println("value for key"
26+
+ " \"vishal\" is:- "
27+
+ a);
28+
}
29+
30+
map.clear();
31+
print(map);
32+
}
33+
34+
public static void print(Map<String, Integer> map)
35+
{
36+
if (map.isEmpty()) {
37+
System.out.println("map is empty");
38+
}
39+
40+
else {
41+
System.out.println(map);
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)