Java How to return top 10 items based on value in a HashMap
Solution 1
- Put the map's
entrySet()
into aList
. - Sort this list using
Collections.sort
and aComparator
which sortsEntry
s based on their values. - Use the
subList(int, int)
method ofList
to retrieve a new list containing the top 10 elements.
Yes, it will be much more verbose than Python :)
Solution 2
With Java 8+, to get the first 10 elements of a list of intergers:
list.stream().sorted().limit(10).collect(Collectors.toList());
To get the first 10 elements of a map's keys, that are integers:
map.keySet().stream().sorted().limit(10).collect(Collectors.toMap(Function.identity(), map::get));
Related videos on Youtube
Eric
Updated on October 06, 2022Comments
-
Eric over 1 year
So I am very new to Java and as such I'm fighting my way through an exercise, converting one of my Python programs to Java.
I have run into an issue where I am trying to replicate the behavior, from python the following will return only the keys sorted (by values), not the values:
popular_numbers = sorted(number_dict, key = number_dict.get, reverse = True)
In Java, I have done a bit of research and have not yet found an easy enough sample for a n00b such as myself or a comparable method. I have found examples using Guava for sorting, but the sort appears to return a HashMap sorted by key.
In addition to the above, one of the other nice things about Python, that I have not found in Java is the ability to, easily, return a subset of the sorted values. In Python I can simply do the following:
print "Top 10 Numbers: %s" % popular_numbers[:10]
In this example, number_dict is a dictionary of key,value pairs where key represents numbers 1..100 and the value is the number of times the number (key) occurs:
for n in numbers: if not n == '': number_dict[n] += 1
The end result would be something like:
Top 10 Numbers: ['27', '11', '5', '8', '16', '25', '1', '24', '32', '20']
To clarify, in Java I have successfully created a HashMap, I have successfully examined numbers and increased the values of the key,value pair. I am now stuck at the sort and return the top 10 numbers (keys) based on value.
-
Zong almost 11 yearsIs there a reason for using
HashMap
, particularly since you need sorting?
-
-
NullUserException almost 11 yearsNow you've lost the key-value pairings
-
arshajii almost 11 years
TreeMap
sorts based on keys not values. -
chessbot almost 11 yearsThis sorts by the values, but doesn't let you easily look them up to see which key they referred to...
-
AnarchoEnte almost 11 yearsYes the binding is lost, but it will work if you only need the first top 10 elements.
-
Zong almost 11 yearsYeah this is the way to go. But then what was the point of using
HashMap
? Especially when the result is ordered by value? I don't know what the OP is doing... -
Zong almost 11 yearsIt would be better to use
b.getValue().compareTo(a.getValue())
in the comparison. The OP's example uses integers, but if they decided to put some floating points in, just changing the types would break this code. -
Paul Vargas almost 11 yearsThe
value
is the popularity of a number (number may be floating). In other words, the frequency. -
Zong almost 11 yearsAlright, I see that in this case it will just be integers. However, I'd still stick with my suggestion.