Tuesday, July 18, 2017

Sort HashMap by Values


When it comes to sorting a hash map by keys, it can be easily implemented using TreeMap as TreeMap by default sorts a map using key.

But when it comes to sort a map by values, we’ve to make use of Comparator which sorts map values by itself.

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

public class HashMapSort {
     public static void main(String[] args) {
           HashMap<String,String> hashMap = new HashMap<>();
          
           /*Entering values in an Array*/
           hashMap.put("Paras","Chawla");
           hashMap.put("Sonal","Gupta");
           hashMap.put("Richa", "Pruthi");
           hashMap.put("Usha","Pahwa");
          
           System.out.println("Unsorted HashMap "+ hashMap);
          
           /*Putting Entry<String,String> into set*/
           Set<Entry<String,String>> set= hashMap.entrySet();
           System.out.println("Unsorted Set "+set);

           /*Creating list using Set*/
           List<Entry<String,String>> list = new ArrayList<>(set);
           System.out.println("Unsorted List "+list);
          
           /*Creating Comparator*/
           Comparator<Entry<String,String>> comparator = new Comparator<Entry<String,String>>() {

                @Override
                public int compare(Entry<String, String> o1, Entry<String, String> o2) {
                     return o1.getValue().compareTo(o2.getValue());
                }
           };
          
           /*Passing list and comparator to Collections.sort()*/
           Collections.sort(list, comparator);
          
           /*Got sorted list from sort()*/
           System.out.println("Sorted List"+list);
          
           /*Since HashMap doesn't maintain order,
            * Adding Entry key and Entry value from List in LHM */
           LinkedHashMap<String,String> linkedHashMap = new LinkedHashMap<>();
          
           /*Adding from List into LinkedHashMap*/
           for(Entry<String,String> entry : list)
                linkedHashMap.put(entry.getKey(),entry.getValue());
               
           /*For printing we need to add LinkedHashMap to Set again */
           Set<Entry<String,String>> sortedSet = linkedHashMap.entrySet();
          
           System.out.println("Sorted Map");
           for(Entry<String,String> entry : sortedSet)
           System.out.println("Key->"+ entry.getKey()+"| Value-->"+ entry.getValue());
     }
}

Output

Unsorted HashMap {Richa=Pruthi, Paras=Chawla, Sonal=Gupta, Usha=Pahwa}
Unsorted Set [Richa=Pruthi, Paras=Chawla, Sonal=Gupta, Usha=Pahwa]
Unsorted List [Richa=Pruthi, Paras=Chawla, Sonal=Gupta, Usha=Pahwa]
Sorted List[Paras=Chawla, Sonal=Gupta, Usha=Pahwa, Richa=Pruthi]
Sorted Map
Key->Paras| Value-->Chawla
Key->Sonal| Value-->Gupta
Key->Usha| Value-->Pahwa
Key->Richa| Value-->Pruthi


No comments:

Post a Comment