How to sort HashMap elements according to their values?

I have the following HashMap :

 HashMap<String, Integer> counts = new HashMap<String, Integer>(); 

What is the easiest way to arrange it according to values?

+4
source share
4 answers

You cannot sort Map values, especially not HashMap , which cannot be sorted at all.

Instead, you can sort the entries:

 List<Map.Entry<String, Integer>> entries = new ArrayList<Map.Entry<String, Integer>>(map.entrySet()); Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() { public int compare( Map.Entry<String, Integer> entry1, Map.Entry<String, Integer> entry2) { return entry1.getValue().compareTo(entry2.getValue()); } }); 

sorts records in ascending order of quantity.

+8
source

You can get a set of records ( Set Map.Entry ) from the map using map.entrySet() . Just getValue() over them and check the getValue() values.

+3
source

A TreeMap can save its records in the order defined by Comparator .

  • We can create a comparator that will order the card, putting the highest value in the first place.
  • Then we will create a TreeMap that uses this Comparator.
  • Then we put all the records in our card counts in the Comparator.
  • Finally, we get the first key on the map, which should be the most common word (or at least one of them, if several words have equal meanings).

     public class Testing { public static void main(String[] args) { HashMap<String,Double> counts = new HashMap<String,Integer>(); // Sample word counts counts.put("the", 100); counts.put("pineapple",5); counts.put("a", 50); // Step 1: Create a Comparator that order by value with greatest value first MostCommonValueFirst mostCommonValueFirst = new MostCommonValueFirst(counts); // Step 2: Build a TreeMap that uses that Comparator TreeMap<String,Double> sortedMap = new TreeMap<String,Integer (mostCommonValueFirst); // Step 3: Populate TreeMap with values from the counts map sortedMap.putAll(counts); // Step 4: The first key in the map is the most commonly used word System.out.println("Most common word: " + sortedMap.firstKey()); } } private class MostCommonValueFirst implements Comparator<String> { Map<String, Integer> base; public MostCommonValueFirst(Map<String, Integer> base) { this.base = base; } // Note: this comparator imposes orderings that are inconsistent with equals. public int compare(String a, String b) { if (base.get(a) >= base.get(b)) { return 1; } else { return -1; } // returning 0 would merge keys } } 

Source: fooobar.com/questions/31 / ...

+1
source

Work if you want them to print them in order (without saving).

  • Create a new map ( tempMap ) and put your value as a key and key as a value. To make keys unique, add a unique value to each of the keys, for example. key1 = value1 + @ 0.

  • Get a list of values ​​as map.values() as a list of myVlues

  • Detach myVlues list as Collections.sort(myVlues)

  • Now iterate myVlues , get the corresponding key from tempMap , restore the key, for example. key.substring (0, key.length-2) and print a pair of keys and values.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1440094/


All Articles