Table of Contents
TreeMap class implements Map similar to HashMap.
Some important points about TreeMap:
- TreeMap implements Map interface and extends HashMap class.
- TreeMap is implemented using Red black tree based NavigableMap.
- TreeMap is ordered collection and store its elements in natural ordering of keys.
- Key which you would like to put in TreeMap must implement Comaparable interface or you can use Comparator for custom sorting
Example:
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 |
package org.arpit.java2blog; import java.util.TreeMap; public class TreeMapMain { public static void main(String args[]) { // TreeMap with Country as key and capital as value // TreeMap stores elements in natural ordering of keys. TreeMap<String,String> countryCapitalMap=new TreeMap<String,String>(); countryCapitalMap.put("India","Delhi"); countryCapitalMap.put("Japan","Tokyo"); countryCapitalMap.put("France","Paris"); countryCapitalMap.put("Russia","Moscow"); System.out.println("-----------------------------"); // Iterating TreeMap Using keySet() and for each loop System.out.println("Iterating TreeMap Using keySet() and for each loop"); for (String countryKey:countryCapitalMap.keySet()) { System.out.println("Country:"+ countryKey +" and Capital:"+countryCapitalMap.get(countryKey)); } System.out.println("-----------------------------"); } } |
1 2 3 4 5 6 7 8 9 |
----------------------------- Iterating TreeMap Using keySet() and for each loop Country:France and Capital:Paris Country:India and Capital:Delhi Country:Japan and Capital:Tokyo Country:Russia and Capital:Moscow ----------------------------- |
What if you want custom sorting rather than natural ordering:
If you want custom sorting , then you can using below TreeMap constructor. You can define your own comparator.
1 2 3 |
TreeMap countryCapitalMap=new TreeMap(Comparator comp); |
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 |
package org.arpit.java2blog; public class Country { 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; } } |
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 39 40 41 42 43 |
package org.arpit.java2blog; import java.util.Comparator; 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); Comparator comparator=new Comparator() { @Override public int compare(Country o1, Country o2) { return o2.getName().compareTo(o1.getName()); } }; System.out.println("Sorting TreeMap in reverse order of country name"); TreeMap<Country, String> countryCapitalMap=new TreeMap<Country,String>(comparator); countryCapitalMap.put(india,"Delhi"); countryCapitalMap.put(japan,"Tokyo"); countryCapitalMap.put(france,"Paris"); countryCapitalMap.put(russia,"Moscow"); Iterator countryCapitalIter=countryCapitalMap.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); } } } |
1 2 3 4 5 6 7 |
Sorting TreeMap in reverse order of country name Russia----Moscow Japan----Tokyo India----Delhi France----Paris |
You can pass HashMap to constructor of TreeMap to sort it on Key
1 2 3 |
TreeMap countryCapitalMap=new TreeMap(Map hm); |
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 |
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,String>(); 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,String> sortedTreeMapCountryCapital=new TreeMap<Country,String> (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); } } } |
1 2 3 4 5 6 7 |
Sorting HashMap by passing it to TreeMap constructor France----Paris India----Delhi Japan----Tokyo Russia----Moscow |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.