-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapExample.java
More file actions
179 lines (156 loc) · 5.09 KB
/
MapExample.java
File metadata and controls
179 lines (156 loc) · 5.09 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package example.JavaCollectionExample;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* HashMap, LinkedHashMap, TreeMap
*
*
* ----------
* | Map | < - - - - - - - - -
* ---------- < \
* ^ ^ \ \
* extends | | \ implements \ implements
* / \ \ \
* / \implements \ \
* ---------- -------- ------------- ---------
* |SortedMap | |HashMap | |LinkedHashMap| |HashTable|
* ---------- -------- ------------- ---------
* ^
* | implements
* ----------
* |TreeMap |
* ----------
*
* Reference:
* - https://www.w3spoint.com/hashmap-linkedhashmap-treemap-hashtable-java
* - https://beginnersbook.com/2014/06/difference-between-hashmap-and-hashtable/
* - https://beginnersbook.com/java-collections-tutorials/
* - https://www.geeksforgeeks.org/iterate-map-java/?ref=leftbar-rightbar
*/
public class MapExample {
public static void main(String[] args) {
// example for HashMap.
HashMapExample hme = new HashMapExample();
hme.example();
hme.hashmapExample();
// hme.hashmapKeySetExample();
// hme.hashmapToIterator();
// example for LinkedHashMap.
LinkedHashMapExample lhme = new LinkedHashMapExample();
lhme.example();
// example for TreeMap.
// TODO: 增加 treeMap example
}
}
/**
* HashMap
* - 元素皆唯一
* - 沒保持當初建立的順序 (order)
* - 每個元素皆有 key, value
* - 允許 null key, null value.
*
* Method:
* - Map.entrySet()
* - Map.Entry<K, V>
*
* Reference:
* - https://beginnersbook.com/2013/12/hashmap-in-java-with-example/
*
*/
class HashMapExample {
void example() {
// * empty, remove, clear example for HashMap.
Map<Integer, String> map = generateData();
System.out.println("content: " + map);
System.out.println("# of element: " + map.size());
System.out.println("Is empty: " + map.isEmpty());
System.out.println("Contain 101: " + map.containsKey(100));
map.remove(102);
System.out.println("content: " + map);
System.out.println(map.get(100));
System.out.println(map.get(102));
map.clear();
System.out.println("content: " + map);
}
public Map<Integer, String> generateData() {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(100, "george");
map.put(101, "may");
map.put(102, "JJ");
return map;
}
public void hashmapExample() {
// * Map.entrySet() entries using for-each loop.
Map<Integer, String> map = generateData();
System.out.println("顯示hashmap資料: ");
System.out.println(map.entrySet());
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", " + "Value: " + entry.getValue());
}
}
public void hashmapKeySetExample() {
// * Map using keySet() and values() methods
Map<Integer, String> map = generateData();
// using keySet() for iteration over keys.
for (Integer id : map.keySet()) {
System.out.println("key: " + id);
}
// using values() for iteration over values.
for (String name : map.values()) {
System.out.println("value: " + name);
}
}
public void hashmapToIterator() {
// * using iterators.
Map<Integer, String> map = generateData();
Iterator<Map.Entry<Integer, String>> itr = map.entrySet()
.iterator();
while(itr.hasNext()) {
Map.Entry<Integer, String> entry = itr.next();
System.out.println("key: " + entry.getKey() + "; value: " + entry.getValue());
}
}
public void useForEachForHashMap() {
// * using foreach method.
Map<Integer, String> map = generateData();
map.forEach( (k, v) -> {
System.out.println("key: " + k + "; value: " + v);
});
}
}
/**
* LinkedHashMap
* - 元素皆唯一
* - 可保持當初建立的順序 (order)
* - 每個元素皆有 key, value
* - 允許 null key, null value.
*
* Reference:
* - https://beginnersbook.com/2013/12/linkedhashmap-in-java/
*/
class LinkedHashMapExample {
void example() {
var map = new LinkedHashMap<String, String>();
map.put("kaohsiung", "高雄");
map.put("taipei", "台北");
map.put("tainan", "台南");
System.out.println(map.get("kaohsiung"));
//! 似乎沒有 map.getKey(), map.getValue()
}
}
/**
* TreeMap
* - 皆為唯一
* - 由大到小排序
* - 不允許空的key, 但允許空的value.
*
* Reference:
* - https://beginnersbook.com/2013/12/treemap-in-java-with-example/
*/
class TreeMapExample {
void example() {
//! 似乎 import 會有問題!
}
}