-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLinkedHashMapOrder.java
57 lines (47 loc) · 1.61 KB
/
LinkedHashMapOrder.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
package collection.demo;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class LinkedHashMapOrder {
public static void main(String[] args) {
LinkedHashMap<String, String> testMap = new LinkedHashMap<>();
testMap.put("1", "One");
testMap.put("2", "Two");
testMap.put("3", "Three");
testMap.put("4", "Four");
int indexOfFourtyFour = -1;
System.out.println("Test Map before :: " + testMap);
Iterator<Entry<String, String>> itr = testMap.entrySet().iterator();
int index = 0;
while (itr.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) itr.next();
if (entry.getKey().equals("3")) {
itr.remove();
indexOfFourtyFour = index;
}
index ++;
}
if (indexOfFourtyFour > -1) {
add(testMap, indexOfFourtyFour, "44", "FourFour");
}
System.out.println("Test Map after :: " + testMap);
}
public static <K, V> void add(Map<K, V> map, int index, K key, V value) {
int i = 0;
List<Entry<K, V>> rest = new ArrayList<Entry<K, V>>();
for (Entry<K, V> entry : map.entrySet()) {
if (i++ >= index) {
rest.add(entry);
}
}
map.put(key, value);
for (int j = 0; j < rest.size(); j++) {
Entry<K, V> entry = rest.get(j);
map.remove(entry.getKey());
map.put(entry.getKey(), entry.getValue());
}
}
}