In my previous post we have learnt How to Sort HashMap in Java by Keys, in this article we will learn to sort HashMap values. We will be using the below three approaches.
- Implementing the Comparator Interface along with TreeMap Collection
- Implementing a separate class implementing Comparator Interface
- Using Collections.sort() method
1. HashMap Sorting by Values Example – Using TreeMap and Comparator
In this example we will sort the values of the HashMap using TreeMap and Comparator. We will be passing the keys to the comparator through which we can get the Value and sort the values.
package com.javainterviewpoint.HashMap; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class SortByValuesHashMapExample { public static void main(String[] args) { final Map<Integer, String> unsortedMap = new HashMap<Integer, String>(); unsortedMap.put(5, "asd"); unsortedMap.put(1, "cfd"); unsortedMap.put(7, "gdf"); unsortedMap.put(55, "qwe"); unsortedMap.put(66, "weq"); unsortedMap.put(3, "wer"); unsortedMap.put(8, "yes"); unsortedMap.put(93, "nsa"); unsortedMap.put(50, "tes"); unsortedMap.put(12, "mds"); unsortedMap.put(43, "fsa"); //Print the Elements of the Map before Sorting System.out.println("Elements of the HashMap before Sorting"); printMap(unsortedMap); Map<Integer,String> sortedMap = new TreeMap<Integer,String>(new Comparator() { @Override public int compare(Integer i1, Integer i2) { return unsortedMap.get(i1).compareTo(unsortedMap.get(i2)); } } ); sortedMap.putAll(unsortedMap); //Print the Elements of the Map after Sorting System.out.println("Elements of the HashMap after Sorting"); printMap(sortedMap); } public static void printMap(Map<Integer, String> map) { System.out.println("**************************************"); for (Map.Entry<Integer, String> entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } System.out.println(); } }
Output :
Elements of the HashMap before Sorting ************************************** Key : 50 Value : tes Key : 1 Value : cfd Key : 3 Value : wer Key : 55 Value : qwe Key : 5 Value : asd Key : 66 Value : weq Key : 7 Value : gdf Key : 93 Value : nsa Key : 8 Value : yes Key : 43 Value : fsa Key : 12 Value : mds Elements of the HashMap after Sorting ************************************** Key : 5 Value : asd Key : 1 Value : cfd Key : 43 Value : fsa Key : 7 Value : gdf Key : 12 Value : mds Key : 93 Value : nsa Key : 55 Value : qwe Key : 50 Value : tes Key : 66 Value : weq Key : 3 Value : wer Key : 8 Value : yes
2. HashMap Sorting by Values Example – Separate Comparator class
Here we will be implementing the comparator interface to a separate class called ValueComparator and we will be passing it to the TreeMap.
package com.javainterviewpoint.HashMap; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class SortByValuesHashMapExample { public static void main(String[] args) { Map<Integer, String> unsortedMap = new HashMap<Integer, String>(); unsortedMap.put(5, "asd"); unsortedMap.put(1, "cfd"); unsortedMap.put(7, "gdf"); unsortedMap.put(55, "qwe"); unsortedMap.put(66, "weq"); unsortedMap.put(3, "wer"); unsortedMap.put(8, "yes"); unsortedMap.put(93, "nsa"); unsortedMap.put(50, "tes"); unsortedMap.put(12, "mds"); unsortedMap.put(43, "fsa"); //Print the Elements of the Map before Sorting System.out.println("Elements of the HashMap before Sorting"); printMap(unsortedMap); Map<Integer,String> sortedMap = new TreeMap<Integer,String>(new ValueComparator(unsortedMap)); sortedMap.putAll(unsortedMap); //Print the Elements of the Map after Sorting System.out.println("Elements of the HashMap after Sorting"); printMap(sortedMap); } public static void printMap(Map<Integer, String> map) { System.out.println("**************************************"); for (Map.Entry<Integer, String> entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } System.out.println(); } } class ValueComparator implements Comparator { Map<Integer,String> unsortedMap; public ValueComparator(Map unsortedMap) { this.unsortedMap = unsortedMap; } @Override public int compare(Integer i1,Integer i2) { return unsortedMap.get(i1).compareTo(unsortedMap.get(i2)); } }
Output :
Elements of the HashMap before Sorting ************************************** Key : 50 Value : tes Key : 1 Value : cfd Key : 3 Value : wer Key : 55 Value : qwe Key : 5 Value : asd Key : 66 Value : weq Key : 7 Value : gdf Key : 93 Value : nsa Key : 8 Value : yes Key : 43 Value : fsa Key : 12 Value : mds Elements of the HashMap after Sorting ************************************** Key : 5 Value : asd Key : 1 Value : cfd Key : 43 Value : fsa Key : 7 Value : gdf Key : 12 Value : mds Key : 93 Value : nsa Key : 55 Value : qwe Key : 50 Value : tes Key : 66 Value : weq Key : 3 Value : wer Key : 8 Value : yes
3. HashMap Sorting by Values Example – Collections.sort() method
In this approach we will be getting the EntrySet and store it to a List(unsortedList)and pass the list along with the comparator to Collections.sort() method. Finally add the sortedList to the LinkedHashMap(sortedMap) as it will maintain the insertion order.
package com.javainterviewpoint.HashMap; 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; import java.util.Map.Entry; public class SortByValuesHashMapExample { public static void main(String[] args) { final Map<Integer, String> unsortedMap = new HashMap<Integer, String>(); unsortedMap.put(5, "asd"); unsortedMap.put(1, "cfd"); unsortedMap.put(7, "gdf"); unsortedMap.put(55, "qwe"); unsortedMap.put(66, "weq"); unsortedMap.put(3, "wer"); unsortedMap.put(8, "yes"); unsortedMap.put(93, "nsa"); unsortedMap.put(50, "tes"); unsortedMap.put(12, "mds"); unsortedMap.put(43, "fsa"); //Print the Elements of the Map before Sorting System.out.println("Elements of the HashMap before Sorting"); printMap(unsortedMap); List<Entry<Integer,String>> unsortedList = new ArrayList<Entry<Integer,String>>(unsortedMap.entrySet()); Collections.sort(unsortedList,new Comparator<Entry<Integer,String>>() { @Override public int compare(Entry<Integer,String> e1, Entry<Integer,String> e2) { return e1.getValue().compareTo(e2.getValue()); } }); Map<Integer,String> sortedMap = new LinkedHashMap<Integer,String>(); for(Entry<Integer,String> entry:unsortedList){ sortedMap.put(entry.getKey(),entry.getValue()); } //Print the Elements of the Map after Sorting System.out.println("Elements of the HashMap after Sorting"); printMap(sortedMap); } public static void printMap(Map<Integer, String> map) { System.out.println("**************************************"); for (Map.Entry<Integer, String> entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } System.out.println(); } }
Output :
Elements of the HashMap before Sorting ************************************** Key : 50 Value : tes Key : 1 Value : cfd Key : 3 Value : wer Key : 55 Value : qwe Key : 5 Value : asd Key : 66 Value : weq Key : 7 Value : gdf Key : 93 Value : nsa Key : 8 Value : yes Key : 43 Value : fsa Key : 12 Value : mds Elements of the HashMap after Sorting ************************************** Key : 5 Value : asd Key : 1 Value : cfd Key : 43 Value : fsa Key : 7 Value : gdf Key : 12 Value : mds Key : 93 Value : nsa Key : 55 Value : qwe Key : 50 Value : tes Key : 66 Value : weq Key : 3 Value : wer Key : 8 Value : yes
How to Sort HashMap having Object Values ?
We have learnt how to sort Wrapper Objects, but in the real world situations you will be in a situation to sort object based on the particular attribute lets now see how we can achieve this.
Lets take a class Car which has two attributes color and wheels, we implement sorting of Car object based on wheels attribute
Car.java
public class Car { private String color; private Integer wheels; public Car(String color, int wheels) { this.color = color; this.wheels = wheels; } public String getColor() { return color; } public Integer getWheels() { return wheels; } @Override public String toString() { return ""+color+""+wheels; } }
SortObjectValueHashMapExample.java
package com.javainterviewpoint.HashMap; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; public class SortObjectValueHashMapExample { public static void main(String args[]) { final Map<String,Car> unsortedMap = new HashMap<String,Car>(); Car c1 = new Car("Red",3); Car c2 = new Car("Blue",1); Car c3 = new Car("Green",4); Car c4 = new Car("Yellow",2); unsortedMap.put("Red Car",c1); unsortedMap.put("Blue Car",c2); unsortedMap.put("Green Car",c3); unsortedMap.put("Yellow Car",c4); //Print the Elements of the Map before Sorting System.out.println("Elements of the HashMap before Sorting"); printMap(unsortedMap); Map<String,Car> sortedMap = new TreeMap<String,Car>( new Comparator() { @Override public int compare(String s1,String s2) { return unsortedMap.get(s1).getWheels().compareTo(unsortedMap.get(s2).getWheels()); } }); sortedMap.putAll(unsortedMap); //Print the Elements of the Map after Sorting System.out.println("Elements of the HashMap after Sorting"); printMap(sortedMap); } public static void printMap(Map<String,Car> map) { System.out.println("**************************************"); for (Entry<String,Car> entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } System.out.println(); } }
Output :
Elements of the HashMap before Sorting ************************************** Key : Green Car Value : Green4 Key : Yellow Car Value : Yellow2 Key : Red Car Value : Red3 Key : Blue Car Value : Blue1 Elements of the HashMap after Sorting ************************************** Key : Blue Car Value : Blue1 Key : Yellow Car Value : Yellow2 Key : Red Car Value : Red3 Key : Green Car Value : Green4
Leave a Reply