-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionUtil.java
42 lines (35 loc) · 1.02 KB
/
CollectionUtil.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
package javaToolkit.lib.utils;
import java.util.*;
public class CollectionUtil {
/**
* https://stackoverflow.com/a/109389/4315608
*
* @param map
* @param <K>
* @param <V>
* @return
*/
public static <K, V> Map<K, V> sortByValue(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, new Comparator<Object>() {
@SuppressWarnings("unchecked")
public int compare(Object o1, Object o2) {
return ((Comparable<V>) ((Map.Entry<K, V>) (o1)).getValue())
.compareTo(((Map.Entry<K, V>) (o2)).getValue());
}
});
Map<K, V> result = new LinkedHashMap<>();
for (Iterator<Map.Entry<K, V>> it = list.iterator(); it.hasNext();) {
Map.Entry<K, V> entry = (Map.Entry<K, V>) it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}
public static Set<Object> mergeMultipleSet(List<Set<Object>> multiSet) {
Set<Object> mergeSet = new HashSet<>();
for (Set<Object> set : multiSet) {
mergeSet.addAll(set);
}
return mergeSet;
}
}