forked from sporst/JHexView
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMapHelpers.java
More file actions
45 lines (41 loc) · 1.1 KB
/
MapHelpers.java
File metadata and controls
45 lines (41 loc) · 1.1 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
package tv.porst.splib.maps;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* Contains helper functions for working with maps.
*/
public final class MapHelpers
{
/**
* Sorts a map by its values.
*
* @param <S>
* Type of map keys.
* @param <T>
* Type of map values.
* @param map
* The map to sort.
*
* @return The sorted map.
*/
public static <S, T extends Comparable<T>> Map<S, T> sortByValue(final Map<S, T> map)
{
final List<Map.Entry<S, T>> list = new LinkedList<Map.Entry<S, T>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<S, T>>() {
@Override
public int compare(final Map.Entry<S, T> o1, final Map.Entry<S, T> o2)
{
return o1.getValue().compareTo(o2.getValue());
}
});
final Map<S, T> result = new LinkedHashMap<S, T>();
for (final Map.Entry<S, T> element : list) {
result.put(element.getKey(), element.getValue());
}
return result;
}
}