This example shows how to print TreeMap in Java. The example also shows how to print all keys, all values, and all entries of TreeMap using for loop and iterator.
How to print TreeMap in Java?
There are several ways using which you can print TreeMap keys, values and entries as given below.
1. How to print all the keys of TreeMap?
First, get all the keys of the TreeMap using the keySet
method and then use the for loop to iterate and print them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Set; import java.util.TreeMap; public class JavaPrintTreeMapExample { public static void main(String[] args) { TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); //get all keys Set<Integer> keys = tmapColors.keySet(); //using for loop System.out.println("Printing all keys of TreeMap"); for(Integer key : keys){ System.out.println( key ); } } } |
You can also use an Iterator as given below.
1 2 3 4 5 6 |
Iterator<Integer> iterator = tmapColors.keySet().iterator(); System.out.println("Printing all keys of TreeMap"); while(iterator.hasNext()){ System.out.println( iterator.next() ); } |
Output
1 2 3 4 |
Printing all keys of TreeMap 1 2 3 |
2. How to print all the values of TreeMap?
First, get all the values of the TreeMap using the values
method and then use the for loop to iterate and print them.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); //get all values Collection<String> values = tmapColors.values(); //using for loop for(String value : values){ System.out.println( value ); } |
You can also use an iterator as given below.
1 2 3 4 5 |
Iterator<String> iterator = tmapColors.values().iterator(); while(iterator.hasNext()){ System.out.println( iterator.next() ); } |
Output
1 2 3 |
Red Green Blue |
3. How to print all the entries of TreeMap?
Get all the entries of the TreeMap using the entrySet
method and then use the for loop to iterate and print them.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); //get all entries Set<Map.Entry<Integer, String>> entries = tmapColors.entrySet(); //using for loop for(Map.Entry<Integer, String> entry : entries){ System.out.println( entry.getKey() + "=>" + entry.getValue() ); } |
You can also use an iterator as given below.
1 2 3 4 5 6 7 8 9 |
Iterator<Map.Entry<Integer, String>> iterator = tmapColors.entrySet().iterator(); Map.Entry<Integer, String> entry = null; while(iterator.hasNext()){ entry = iterator.next(); System.out.println( entry.getKey() + "=>" + entry.getValue() ); } |
Output
1 2 3 |
1=>Red 2=>Green 3=>Blue |
4. How to print TreeMap having custom class objects as keys or values?
Let’s first see an example of printing a TreeMap object having objects of a custom class Emp as values.
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 |
import java.util.Map; import java.util.Set; import java.util.TreeMap; class Emp{ private Integer id; private String name; public Emp(Integer id, String name){ this.id = id; this.name = name; } public Integer getId() { return id; } public String getName() { return name; } } public class JavaPrintTreeMapExample { public static void main(String[] args) { TreeMap<Integer, Emp> tmapEmployees = new TreeMap<Integer, Emp>(); tmapEmployees.put(1, new Emp(1, "Ryan")); tmapEmployees.put(2, new Emp(2, "Rita")); tmapEmployees.put(3, new Emp(3, "Rajesh")); //get all entries Set<Map.Entry<Integer, Emp>> entries = tmapEmployees.entrySet(); //using for loop for(Map.Entry<Integer, Emp> entry : entries){ System.out.println( entry.getKey() + "=>" + entry.getValue() ); } } } |
Output
1 2 3 |
1=>com.javacodeexamples.collections.treemap.Emp@15db9742 2=>com.javacodeexamples.collections.treemap.Emp@6d06d69c 3=>com.javacodeexamples.collections.treemap.Emp@7852e922 |
Well, it did not print any useful information at all. The reason is our Emp class has not overridden the toString
method from the Object class. In that case, the toString
method of the Object class is used which prints “class_name@object_hashcode” when an object is printed.
To fix this, we need to override the toString
method in our Emp class. Let’s do that and try again.
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 44 45 46 47 48 49 50 |
import java.util.Map; import java.util.Set; import java.util.TreeMap; class Emp{ private Integer id; private String name; public Emp(Integer id, String name){ this.id = id; this.name = name; } public Integer getId() { return id; } public String getName() { return name; } /* * Override this method in your custom class * used as key or value in the TreeMap */ public String toString(){ return "[" + this.getId() + "=>" + this.getName() + "]"; } } public class JavaPrintTreeMapExample { public static void main(String[] args) { TreeMap<Integer, Emp> tmapEmployees = new TreeMap<Integer, Emp>(); tmapEmployees.put(1, new Emp(1, "Ryan")); tmapEmployees.put(2, new Emp(2, "Rita")); tmapEmployees.put(3, new Emp(3, "Rajesh")); //get all entries Set<Map.Entry<Integer, Emp>> entries = tmapEmployees.entrySet(); //using for loop for(Map.Entry<Integer, Emp> entry : entries){ System.out.println( entry.getKey() + "=>" + entry.getValue() ); } } } |
Output
1 2 3 |
1=>[1=>Ryan] 2=>[2=>Rita] 3=>[3=>Rajesh] |
As you can see from the output, this time it printed the useful information.
Tip: Always override the toString
method in your custom classes.
This example is a part of the Java TreeMap Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeMap