This example shows how to print TreeSet in Java. The example also shows how to print all elements (display all elements) of the TreeSet of custom class objects.
How to print TreeSet elements in Java?
There are several ways using which we can print TreeSet elements or display TreeSet elements as given below.
1. Using the enhanced for loop
We can use the enhanced for loop to iterate over the TreeSet and print the elements one by one as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.TreeSet; public class PrintTreeSetExample { public static void main(String[] args) { TreeSet<Integer> tSetPrimeNumbers = new TreeSet<Integer>(); tSetPrimeNumbers.add(2); tSetPrimeNumbers.add(3); tSetPrimeNumbers.add(5); tSetPrimeNumbers.add(7); tSetPrimeNumbers.add(11); /* * Print elements using the enhanced for loop */ for(Integer number : tSetPrimeNumbers){ System.out.println(number); } } } |
Output
1 2 3 4 5 |
2 3 5 7 11 |
2. Using the forEach (Java 8)
If you are using Java version 8 or later, you can use the forEach instead of the enhanced for loop as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
TreeSet<Integer> tSetPrimeNumbers = new TreeSet<Integer>(); tSetPrimeNumbers.add(2); tSetPrimeNumbers.add(3); tSetPrimeNumbers.add(5); tSetPrimeNumbers.add(7); tSetPrimeNumbers.add(11); /* * Print elements using the forEach */ tSetPrimeNumbers.forEach( element -> { System.out.println(element); }); |
Output
1 2 3 4 5 |
2 3 5 7 11 |
3. Using the Iterator
We can also the TreeSet Iterator as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
TreeSet<Integer> tSetPrimeNumbers = new TreeSet<Integer>(); tSetPrimeNumbers.add(2); tSetPrimeNumbers.add(3); tSetPrimeNumbers.add(5); tSetPrimeNumbers.add(7); tSetPrimeNumbers.add(11); //get the TreeSet Iterator Iterator<Integer> itr = tSetPrimeNumbers.iterator(); while(itr.hasNext()){ System.out.println( itr.next() ); } |
Output
1 2 3 4 5 |
2 3 5 7 11 |
4. Using the Arrays class
The Arrays class provides the toString
method to display the array. But before we can use that, we need to convert TreeSet to an array as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
TreeSet<Integer> tSetPrimeNumbers = new TreeSet<Integer>(); tSetPrimeNumbers.add(2); tSetPrimeNumbers.add(3); tSetPrimeNumbers.add(5); tSetPrimeNumbers.add(7); tSetPrimeNumbers.add(11); //convert TreeSet to an array Integer[] array = tSetPrimeNumbers.toArray( new Integer[tSetPrimeNumbers.size()] ); //display array System.out.println( Arrays.toString(array) ); |
Output
1 |
[2, 3, 5, 7, 11] |
Note: Converting TreeSet to an array just for the display purpose is not recommended especially if the TreeSet is large. It may not perform well as allocating a new array with TreeSet elements is a costly operation in terms of performance.
How to print elements of TreeSet having custom class objects?
When we try to print a TreeSet of a custom class, the console does not display the useful information. Let’s see an example of that.
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 |
import java.util.TreeSet; class User implements Comparable<User>{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } public int compareTo(User otherUser) { return this.getId() - otherUser.getId(); } } public class PrintTreeSetExample { public static void main(String[] args) { TreeSet<User> tSetUsers = new TreeSet<User>(); tSetUsers.add( new User(1) ); tSetUsers.add( new User(2) ); System.out.println(tSetUsers); } } |
Output
1 |
[com.javacodeexamples.collections.treeset.User@6d06d69c, com.javacodeexamples.collections.treeset.User@7852e922] |
As we can see from the output, the information printed on the console is not useful at all. That is because whenever we print the object, its toString
method is called.
Since the User class has not overridden it, the toString
method inherited from the Object class is used that prints object information in “class_name@hash_code” format.
If we want to return useful information whenever the object is printed, we need to override the toString
method in the 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 |
import java.util.TreeSet; class User implements Comparable<User>{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } public int compareTo(User otherUser) { return this.getId() - otherUser.getId(); } /* * Override this method to return useful * information when this object is printed */ public String toString(){ return "User -> " + getId(); } } public class PrintTreeSetExample { public static void main(String[] args) { TreeSet<User> tSetUsers = new TreeSet<User>(); tSetUsers.add( new User(1) ); tSetUsers.add( new User(2) ); System.out.println(tSetUsers); } } |
Output
1 |
[User -> 1, User -> 2] |
As we can see from the output, the toString
method is called each time the object is printed in the console.
This example is a part of the Java TreeSet Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeSet