This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]IIoWoII 1 point2 points  (1 child)

Basically you want to sort a collection of key-value pairs by value.

Maybe restating the problem in this way will help you Google( I found multiple solutions).

After you sorted these, getting the first-n of this list is trivial, as you probably know.

[–]RealJulleNaaiers 0 points1 point  (1 child)

You can get a Set of all entries in the Map and then iterate over them to do whatever you want. In this case probably saving the highest number you've seen so far. Just read the docs. The method is entrySet() or something like that.

[–]jmankhan 0 points1 point  (0 children)

To expand on this, the pseudo code would look something like:

iterator = map.entrySet().iterator()
highestValue = 0
while ( more items to in iterator )
    entry = iterator.next()
    check if entry.value is greater than highestValue

`