I'm trying to sort a HashMap
based on values, so that its ordered in descending order. But I have no idea on how to implement it. How would I go about doing it?
HashMap<K, Integer> keysAndSizeMap = new HashMap<>();
for (K set : map.keySet()) {
keysAndSizeMap.put(set, map.get(set).size());
}
// implementation here?
System.out.println("keysAndSizeMap: " + keysAndSizeMap);
Example of the result I want:
- input:
{800=12, 90=15, 754=20}
- output:
{754=20, 90=15, 800=12}
-or-
- input:
{"a"=2, "b"=6, "c"=4}
- output:
{"b"=6, "c"=4, "a"=2}