forked from DSCTOCE/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hashmap.java
49 lines (32 loc) · 1.33 KB
/
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
44
45
46
47
48
49
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class Hashmap
{
public static void main(String[] args)
{
//Creating the HashMap
HashMap<String, String> map = new HashMap<String, String>();
//Adding key-value pairs to HashMap
map.put("ONE", "AAA");
map.put("TWO", "BBB");
map.put("THREE", "CCC");
map.put("FOUR", "DDD");
map.put("FIVE", "EEE");
//Printing Key-value pairs
System.out.println("HashMap Before Replace :");
Set<Entry<String, String>> keyValueSet = map.entrySet();
for (Entry<String, String> entry : keyValueSet)
{
System.out.println(entry.getKey()+" : "+entry.getValue());
}
System.out.println("------------------");
//Replacing the value associated with 'FOUR' to '444' only if it is currently mapped to 'DDD'
map.replace("FOUR", "DDD", "444");
System.out.println("HashMap After Replace :");
for (Entry<String, String> entry : keyValueSet)
{
System.out.println(entry.getKey()+" : "+entry.getValue());
}
}
}