Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting the Map<Key,Value> in descending order based on the value [duplicate]

Tags:

java

hashmap

map

Possible Duplicate:
How to sort a Map<Key, Value> on the values in Java?

I am using map interface to read from a file and then store the values in that as a key value pair. The file format is as follows

 A 34  B 25  c 50 

I will read the datas from this file and store that as a key value pair and then I will display this to the user. My requirement is to display the results in this format

C 50 A 34 B 25 

Thus I need to sort the map in descending order of the value. So that I will be able to display these as my result .. I have read about this and find the below code

static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {         SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(             new Comparator<Map.Entry<K,V>>() {                 @Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {                     int res = e1.getValue().compareTo(e2.getValue());                     return res != 0 ? res : 1; // Special fix to preserve items with equal values                 }             }         );         sortedEntries.addAll(map.entrySet());         return sortedEntries;     } 

I hope this is gonna sort the values in ascending order, I just want to know whether this approach is correct or some other effective approach will be helpful for me ?

like image 661
NandaKumar Avatar asked Jul 25 '12 10:07

NandaKumar


People also ask

How do you sort a map in descending order by values?

Use Collectors. toMap() method to get the result in another Map. In order to sort in decreasing order, just reverse the order of Comparator using Collections. reverseOrder() or Comparator.

How do you sort a map by both keys and value?

The idea is to put all data of HashMap into an ArrayList. Then extract all the keys of HashMap into an ArrayList. Next, sort the extracted keys using the Collections. sort() method, and then for each key extract its value using the get() method.


1 Answers

Since you can have duplicate values you shouldn't be using a Set at all. Change to a List and sort it instead. Your entriesSortedByValues would look something like this:

static <K,V extends Comparable<? super V>>              List<Entry<K, V>> entriesSortedByValues(Map<K,V> map) {      List<Entry<K,V>> sortedEntries = new ArrayList<Entry<K,V>>(map.entrySet());      Collections.sort(sortedEntries,              new Comparator<Entry<K,V>>() {                 @Override                 public int compare(Entry<K,V> e1, Entry<K,V> e2) {                     return e2.getValue().compareTo(e1.getValue());                 }             }     );      return sortedEntries; } 

Note: in your example output the values is descending. If you want them ascending, use e1.getValue().compareTo(e2.getValue()) instead.


Example:

public static void main(String args[]) {      Map<String, Integer> map = new HashMap<String, Integer>();     map.put("A", 34);     map.put("B", 25);     map.put("C", 50);     map.put("D", 50); // "duplicate" value      System.out.println(entriesSortedByValues(map)); } 

Output:

[D=50, C=50, A=34, B=25] 
like image 114
dacwe Avatar answered Sep 21 '22 05:09

dacwe