Table of Contents
HashMap does not preserve order of element but what if you want to sort it by keys or values. In this post, we will see how to sort HashMap by keys or values.
Java HashMap tutorial:
- HashMap in java
- HashMap internal working
- hash and indexfor method in HashMap
- hashcode and equals in java
- sort HashMap by keys and values
- Difference between HashMap and HashSet
- Difference between HashMap and Hashtable
- How to iterate over HashMap
Sorting by Keys:
Sorting by keys can be easily done using TreeMap. You just need to pass HashMap to the constructor of TreeMap.
1 2 3 |
TreeMap map=new TreeMap(Map hm); |
Sorting by values:
We can use Comparator to sort it by values.
Sorting by Keys example :
Create Country.java as below. It should implement Comparable interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package org.arpit.java2blog; public class Country implements Comparable { String name; long population; public Country(String name, long population) { super(); this.name = name; this.population = population; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getPopulation() { return population; } public void setPopulation(long population) { this.population = population; } @Override public int compareTo(Object o) { Country country=(Country) o; return this.getName().compareTo(country.getName()); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package org.arpit.java2blog; import java.util.HashMap; import java.util.Iterator; import java.util.TreeMap; public class TreeMapCompMain { public static void main(String[] args) { Country india=new Country("India",1000); Country japan=new Country("Japan",10000); Country france=new Country("France",2000); Country russia=new Country("Russia",20000); HashMap<country string> countryCapitalMap=new HashMap<country>(); countryCapitalMap.put(india,"Delhi"); countryCapitalMap.put(japan,"Tokyo"); countryCapitalMap.put(france,"Paris"); countryCapitalMap.put(russia,"Moscow"); System.out.println("Sorting HashMap by passing it to TreeMap constructor"); TreeMap<country> sortedTreeMapCountryCapital=new TreeMap<country> (countryCapitalMap); Iterator countryCapitalIter=sortedTreeMapCountryCapital.keySet().iterator();//put debug point at this line while(countryCapitalIter.hasNext()) { Country countryObj=countryCapitalIter.next(); String capital=countryCapitalMap.get(countryObj); System.out.println(countryObj.getName()+"----"+capital); } } } </country></country></country></country> |
1 2 3 4 5 6 7 |
Sorting HashMap by passing it to TreeMap constructor France----Paris India----Delhi Japan----Tokyo Russia----Moscow |
Sorting by values example:
Here we will sort HashMap by its values. Here I am taking HashMap
Example:
Explanation:
- Extract key set from HashMap using keySet() method.
- Create ArrayList from above set
- Sort above ArrayList using Comparator
- Create LinkedHashMap from Sorted list