-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Hashmap.java
43 lines (34 loc) · 1023 Bytes
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
// Java program to illustrate HashMap class of java.util
// package
// Importing HashMap class
import java.util.HashMap;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Create an empty hash map by declaring object
// of string and integer type
HashMap<String, Integer> map = new HashMap<>();
// Adding elements to the Map
// using standard put() method
map.put("vishal", 10);
map.put("sachin", 30);
map.put("vaibhav", 20);
// Print size and content of the Map
System.out.println("Size of map is:- "
+ map.size());
// Printing elements in object of Map
System.out.println(map);
// Checking if a key is present and if
// present, print value by passing
// random element
if (map.containsKey("vishal")) {
// Mapping
Integer a = map.get("vishal");
// Printing value for the corresponding key
System.out.println("value for key"
+ " \"vishal\" is:- " + a);
}
}
}