Sorting HashMap by Values including duplicate in Java

 

We have seen sorting HashMap based on custom objects by implementing Comparator and Comparable interfaces. Now lets see how to sort HashMap using values which has duplicate. Can be sorted with multiple ways like using Comparator interface, using Stream from Java 8 etc.,

Sorting HashMap based on values


Sorting HashMap based on custom objects

Now lets see simple example to sort HashMap based on value which has dupilcate using Comparator interface.

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class SortingHashMap {

 public static void main(String[] args) {

  Map<String, Integer> map = new HashMap<String, Integer>();
  map.put("java", 100);
  map.put(".net", 80);
  map.put("python", 99);
  map.put("C++", 65);
  map.put("C", 70);
  map.put("Orale", 65);
  
  System.out.println("Before Sorting : "+map);
  
  map = new SortingHashMap().sortMapByValue(map);
  
  System.out.println("After Sorting  : "+map);
 }

 private Map<String, Integer> sortMapByValue(Map<String, Integer> map) {

  List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(map.entrySet());
  /*
   * Sorting map values by using Comparator
   */
  Collections.sort(list, new Comparator<Entry<String, Integer>>() {
   public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
    return o1.getValue().compareTo(o2.getValue());
   }
  });

  // LinkedMap to maintain the sorted value order 
  Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
  for (Entry<String, Integer> entry : list) {
   sortedMap.put(entry.getKey(), entry.getValue());
  }

  return sortedMap;
 }
}


OUTPUT:

Before Sorting : {python=99, C++=65, java=100, C=70, .net=80, Orale=65}
After Sorting  : {C++=65, Orale=65, C=70, .net=80, python=99, java=100}




Next we will see how to sort HashMap based on value using stream from Java 8


import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

public class SortingHashMap {

 public static void main(String[] args) {

  Map<String, Integer> map = new HashMap<String, Integer>();
  map.put("java", 100);
  map.put(".net", 80);
  map.put("python", 99);
  map.put("C++", 65);
  map.put("C", 70);
  map.put("Orale", 65);
  
  System.out.println("Before Sorting : "+map);
  
  map = new SortingHashMap().sortMapByValue(map);
  
  System.out.println("After Sorting  : "+map);
 }

 private Map<String, Integer> sortMapByValue(Map<String, Integer> map) {

  Map<String, Integer> sortedMap = 
        map.entrySet().stream()
       .sorted(Entry.comparingByValue())
       .collect(Collectors.toMap(Entry::getKey, Entry::getValue,
                           (o1, o2) -> o1, LinkedHashMap::new));
  
  return sortedMap;
 }
}


OUTPUT:


Before Sorting : {python=99, C++=65, java=100, C=70, .net=80, Orale=65}
After Sorting  : {C++=65, Orale=65, C=70, .net=80, python=99, java=100}

No comments:
Write comments