-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMapDemo.java
More file actions
49 lines (40 loc) · 1.41 KB
/
MapDemo.java
File metadata and controls
49 lines (40 loc) · 1.41 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
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapDemo {
public static void main(String[] args) {
/*
* List - Index Wise
* Set - No Duplicate
* Map - Key and Value Pair
* Key can't be duplicate
*/
HashMap<String,Integer> phoneMap = new HashMap<>();
phoneMap.put("amit",2222); // add a new key
phoneMap.put("ram", 3333);
phoneMap.put("amit", 9999);
System.out.println("PHONE MAP is "+phoneMap);
System.out.println(phoneMap.get("amit"));// access key value
//phoneMap.remove("ram"); //remove by key
// How to Traverse the Map
Set<String> keys = phoneMap.keySet();
// Getting all the Keys and Keys are Unique
// so Unique keys will store in Set (Unique)
// and set has Iterator for Traversing
// Now i am Iterating Keys one by one
Iterator<String> keysIterator = keys.iterator(); // Getting Iterator from Set
while(keysIterator.hasNext()){
String key = keysIterator.next();
// give the current key and move to the next key
Integer value = phoneMap.get(key);
System.out.println("Key is "+key +" and Value is "+value);
}
System.out.println("Now From Java 5 ");
// Map is a Interface
// and Map has a Inner Interface which is Entry
for(Map.Entry<String, Integer> keyValuePair : phoneMap.entrySet()){
System.out.println(keyValuePair.getKey()+" "+keyValuePair.getValue());
}
}
}