Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Last active December 7, 2020 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytecodeman/abec88b45de7c712bf2162e2f3fc1a35 to your computer and use it in GitHub Desktop.
Save bytecodeman/abec88b45de7c712bf2162e2f3fc1a35 to your computer and use it in GitHub Desktop.
Supplemental Code for Names App V2.0
int count = 0;
for (Map.Entry<String, Integer> entry : list.entrySet()) {
String key = entry.getKey();
int value = entry.getValue();
if (count >= top)
break;
topNames.add(new Name(key, "M", value));
count++;
}
private static <K, V extends Comparable<V>> TreeMap<K, V> sortByValues(Map<K, V> map) {
Comparator<K> valueComparator = new Comparator<K>() {
public int compare(K k1, K k2) {
int compare = map.get(k1).compareTo(map.get(k2));
return -compare;
}
};
TreeMap<K, V> sortedByValues = new TreeMap<>(valueComparator);
sortedByValues.putAll(map);
return sortedByValues;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment