sort values in map java code example

Example 1: Java sort Map by values

//----------------------------------------------------------------------
// in ascending order:
Map<String, Integer> unSortedMap = getUnSortedMap();
         
System.out.println("Unsorted Map : " + unSortedMap);
 
//LinkedHashMap preserve the ordering of elements in which they are inserted
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
 
unSortedMap.entrySet()
    .stream()
    .sorted(Map.Entry.comparingByValue())
    .forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
 
System.out.println("Sorted Map   : " + sortedMap);
 
Output:
 
Unsorted Map : {alex=1, charles=4, david=2, brian=5, elle=3}
Sorted Map   : {alex=1, david=2, elle=3, charles=4, brian=5}


//----------------------------------------------------------------------
// in descending order:
Map<String, Integer> unSortedMap = getUnSortedMap();
         
System.out.println("Unsorted Map : " + unSortedMap);
 
//LinkedHashMap preserve the ordering of elements in which they are inserted
LinkedHashMap<String, Integer> reverseSortedMap = new LinkedHashMap<>();
 
//Use Comparator.reverseOrder() for reverse ordering
unSortedMap.entrySet()
    .stream()
    .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) 
    .forEachOrdered(x -> reverseSortedMap.put(x.getKey(), x.getValue()));
 
System.out.println("Reverse Sorted Map   : " + reverseSortedMap);
 
Output:
 
Unsorted Map        : {alex=1, charles=4, david=2, brian=5, elle=3}
Reverse Sorted Map  : {brian=5, charles=4, elle=3, david=2, alex=1}

Example 2: Sorting HashMap by values in java

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;
public class SortHashMapByValue
{
   public static void main(String[] args)
   {
      HashMap<String, Integer> hash = new HashMap<String, Integer>();
      hash.put("Toyota", 78);
      hash.put("Skoda", 69);
      hash.put("Honda", 93);
      hash.put("Audi", 59);
      hash.put("Chevrolet", 39);
      hash.put("Hyundai", 56);
      Map<String, Integer> map = sortByValue(hash);
      System.out.println("Sorting hashmap by values in java: ");
      // printing sorted HashMap
      for(Map.Entry<String, Integer> me : map.entrySet())
      {
         System.out.println("Key = " + me.getKey() + ", Value = " + me.getValue());
      }
   }
   public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)
   {
      // creating list from elements of HashMap
      List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(hm.entrySet());
      // sorting list
      Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
      {
         public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2)
         {
            return (o1.getValue()).compareTo(o2.getValue());
         }
      });
      HashMap<String, Integer> ha = new LinkedHashMap<String, Integer>();
      for(Map.Entry<String, Integer> me : list)
      {
         ha.put(me.getKey(), me.getValue());
      }
      return ha;
   }
}

Tags:

Java Example