I am trying to figure out how could I get the top 10 values from the HashMap
. I was initially trying to use the TreeMap
and have it sort by value and then take the first 10 values however it seems that that is not the option, as TreeMap
sorts by key.
I want to still be able to know which keys have the highest values, the K, V
of the map are String, Integer
.
Maybe you should implement the Comparable
Interface to your value objects stored in the hashmap.
Then you can create a array list of all values:
List<YourValueType> l = new ArrayList<YourValueType>(hashmap.values());
Collection.sort(l);
l = l.subList(0,10);
Regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With