forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMapExample.java
More file actions
67 lines (56 loc) · 1.22 KB
/
HashMapExample.java
File metadata and controls
67 lines (56 loc) · 1.22 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.examplehub.basics;
import java.util.HashMap;
import java.util.Map;
public class HashMapExample {
public static void main(String[] args) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("Java", "Easy");
hashMap.put("Python", "So easy");
hashMap.put("C", "normal");
/*
* {Java=Easy, C=normal, Python=So easy}
*/
System.out.println(hashMap);
/*
* So easy
*/
System.out.println(hashMap.get("Python"));
/*
* Easy
*/
System.out.println(hashMap.remove("Java"));
/*
* false
*/
System.out.println(hashMap.remove("Python", "so easy"));
/*
* {C=normal, Python=So easy}
*/
System.out.println(hashMap);
/*
* 2
*/
System.out.println(hashMap.size());
/*
* C
* Python
*/
for (String key : hashMap.keySet()) {
System.out.println(key);
}
/*
* normal
* So easy
*/
for (String value : hashMap.values()) {
System.out.println(value);
}
/*
* C : normal
* Python : So easy
*/
for (Map.Entry<String, String> entry : hashMap.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}