|
|
|
@ -96,6 +96,24 @@ public class Util {
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ValueThenKeyComparator<K extends Comparable<? super K>,
|
|
|
|
|
V extends Comparable<? super V>>
|
|
|
|
|
implements Comparator<Map.Entry<K, V>> {
|
|
|
|
|
|
|
|
|
|
public int compare(Map.Entry<K, V> a, Map.Entry<K, V> b) {
|
|
|
|
|
int cmp1 = a.getValue().compareTo(b.getValue());
|
|
|
|
|
if (cmp1 != 0) {
|
|
|
|
|
return cmp1;
|
|
|
|
|
} else {
|
|
|
|
|
return a.getKey().compareTo(b.getKey());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sorts a map in a descending order by value.
|
|
|
|
|
*/
|
|
|
|
@ -122,12 +140,17 @@ public class Util {
|
|
|
|
|
limit = map.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<K, V> result = new LinkedHashMap<>();
|
|
|
|
|
TimeWatch watch = TimeWatch.start();
|
|
|
|
|
|
|
|
|
|
Stream<Map.Entry<K, V>> st = map.entrySet().stream();
|
|
|
|
|
// sort by alphabet
|
|
|
|
|
Map<K, V> alphaSorted = new LinkedHashMap<>();
|
|
|
|
|
map.entrySet().stream().sorted((a, b) -> ((MultipleHMKeys)a.getKey()).compareTo((MultipleHMKeys)b.getKey())).limit(limit)
|
|
|
|
|
.forEachOrdered(e -> alphaSorted.put(e.getKey(), e.getValue()));
|
|
|
|
|
|
|
|
|
|
st.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(limit)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Map<K, V> result = new LinkedHashMap<>();
|
|
|
|
|
alphaSorted.entrySet().stream().sorted((a, b) -> (int) ((Long) b.getValue() - (Long) a.getValue())).limit(limit)
|
|
|
|
|
.forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
|
|
|
|
|
|
|
|
|
|
if (Settings.PRINT_LOG) {
|
|
|
|
|