-
Notifications
You must be signed in to change notification settings - Fork 3
/
Map_Java.java
60 lines (48 loc) · 1.92 KB
/
Map_Java.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
/*
* => Để có thể lưu trữ được thứ tự các phần tử của Map, chúng ta phải sử dụng LinkedHashMap.
*
* */
package src.java.Sorting.Sort;
import java.util.*;
import java.util.Map.Entry;
public class Map_Java {
public static void main(String[] args) {
// Original data
Map<Integer, String> map = new HashMap<>();
map.put(44, "Four");
map.put(22, "Two");
map.put(33, "Three");
map.put(55, "Five");
map.put(11, "One");
// Convert list to map
List<Entry<Integer, String>> list1 = new ArrayList<>(map.entrySet());
// Create a comparator to sort by key
Comparator<Entry<Integer, String>> keyComparator = new Comparator<Entry<Integer, String>>() {
@Override
public int compare(Entry<Integer, String> o1, Entry<Integer, String> o2) {
return o1.getKey().compareTo(o2.getKey());
}
};
// Sorting a List
Collections.sort(list1, keyComparator);
/////////////////////////////////////////////////
// Convert list to map
List<Entry<Integer, String>> list2 = new ArrayList<>(map.entrySet());
// Create a comparator to sort by value
Comparator<Entry<Integer, String>> valueComparator = new Comparator<Entry<Integer, String>>() {
@Override
public int compare(Entry<Integer, String> o1, Entry<Integer, String> o2) {
return o1.getValue().compareTo(o2.getValue());
}
};
// Sorting a List
Collections.sort(list2, valueComparator);
// Convert List to Map
Map<Integer, String> sortedMap = new LinkedHashMap<>();
for (Entry<Integer, String> entry : list1) {
sortedMap.put(entry.getKey(), entry.getValue());
}
System.out.println("Original map: " + map);
System.out.println("Sorted map: " + sortedMap);
}
}