Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

sorting

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.

like image 568
Eric Avatar asked Jun 13 '13 19:06

Eric


People also ask

How HashMap is given on basis of value?

If we need to sort the HashMap by values, we should create a Comparator. It compares two elements based on the values. After that get the Set of elements from the Map and convert Set into the List.

How do you return a HashMap value in Java?

HashMap get() Method in Java HashMap. get() method of HashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key.

What HashMap put method returns?

Return Value: If an existing key is passed then the previous value gets returned. If a new pair is passed, then NULL is returned.


2 Answers

  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 :)

like image 164
arshajii Avatar answered Oct 02 '22 22:10

arshajii


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));
like image 38
i000174 Avatar answered Oct 02 '22 22:10

i000174