-
-
Save bytecodeman/abec88b45de7c712bf2162e2f3fc1a35 to your computer and use it in GitHub Desktop.
Supplemental Code for Names App V2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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