Java How to return top 10 items based on value in a HashMap

18,804

Solution 1

  1. Put the map's entrySet() into a List.
  2. Sort this list using Collections.sort and a Comparator which sorts Entrys based on their values.
  3. Use the subList(int, int) method of List 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));
Share:
18,804

Related videos on Youtube

Eric
Author by

Eric

Updated on October 06, 2022

Comments

  • Eric
    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
      Zong almost 11 years
      Is there a reason for using HashMap, particularly since you need sorting?
  • NullUserException
    NullUserException almost 11 years
    Now you've lost the key-value pairings
  • arshajii
    arshajii almost 11 years
    TreeMap sorts based on keys not values.
  • chessbot
    chessbot almost 11 years
    This sorts by the values, but doesn't let you easily look them up to see which key they referred to...
  • AnarchoEnte
    AnarchoEnte almost 11 years
    Yes the binding is lost, but it will work if you only need the first top 10 elements.
  • Zong
    Zong almost 11 years
    Yeah 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
    Zong almost 11 years
    It 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
    Paul Vargas almost 11 years
    The value is the popularity of a number (number may be floating). In other words, the frequency.
  • Zong
    Zong almost 11 years
    Alright, I see that in this case it will just be integers. However, I'd still stick with my suggestion.