-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMapExample.java
More file actions
38 lines (30 loc) · 1.03 KB
/
HashMapExample.java
File metadata and controls
38 lines (30 loc) · 1.03 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
package collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class HashMapExample {
public static void main(String[] args) {
/* This is how to declare HashMap */
HashMap<Integer, String> hmap = new HashMap<Integer, String>();
/* Adding elements to HashMap */
hmap.put(12, "Core Java");
hmap.put(2, "Adv Java");
hmap.put(7, "Spring");
hmap.put(49, "Hibernate");
hmap.put(3, "Maven");
/* Get values based on key */
String var = hmap.get(2); // Key:2
System.out.println("Value for key:2 is: " + var + "\n");
/* Remove values based on Key:3 */
hmap.remove(3);
/* Display content using Iterator */
Set<Entry<Integer, String>> set = hmap.entrySet();
Iterator<Entry<Integer, String>> iterator = set.iterator();
while (iterator.hasNext()) {
Entry<Integer, String> me = iterator.next();
System.out.print("Key is: " + me.getKey() + " & Value is: " + me.getValue() + "\n");
}
}
}