-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapEntry_LinkedHashMap.java
78 lines (59 loc) · 2.21 KB
/
MapEntry_LinkedHashMap.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
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
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class MapEntry_LinkedHashMap {
public static void main(String[] args) throws Exception {
// headMap
Map<String, String> map = new LinkedHashMap<>();
map.put("1", "one");
map.put("2", "two");
map.put("3", "three");
map.put("4", "four");
map.put("5", "five");
map.put("6", "six");
map.put("7", "seven");
map.put("8", "eight");
map.put("9", "nine");
map.put("10", "ten");
System.out.println("Map:" + map);
//#1
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("Key:" + entry.getKey() + " Value:" + entry.getValue());
}
for (Entry<String, String> entry : map.entrySet()) {
System.out.println("Key:" + entry.getKey() + " Value:" + entry.getValue());
}
// #2
Set<Map.Entry<String, String>> entries = map.entrySet();
System.out.println("Entry Set:" + entries);
Set<Entry<String, String>> entries1 = map.entrySet();
System.out.println("Entry Set:" + entries1);
LinkedHashMap<String, String> map2 = new LinkedHashMap<>(){
@Override
protected boolean removeEldestEntry(Map.Entry<String, String> eldest) {
return size() > 3;
}
};
map2.put("1", "one");
map2.put("2", "two");
map2.put("3", "three");
map2.put("4", "four");
map2.put("5", "five");
map2.put("6", "six");
System.out.println("Map2:" + map2);
LinkedHashMap<String, String> map3 = new LinkedHashMap<>() {
@Override
protected boolean removeEldestEntry(Entry<String, String> eldest) {
return size() > 3;
}
};
map3.put("1", "one");
map3.put("2", "two");
map3.put("3", "three");
map3.put("4", "four");
map3.put("5", "five");
map3.put("6", "six");
System.out.println("Map3:" + map3);
}
}