This example shows how to create new TreeSet objects in Java using the TreeSet constructors. The example also shows how to use different TreeSet constructors.
How to create new TreeSet objects in Java using TreeSet constructors?
The TreeSet class in Java provides several constructors using which you can create new TreeSet objects.
1. Create new and empty TreeSet object
The default constructor of the TreeSet class creates a new empty TreeSet object.
1 |
public TreeSet() |
The elements of this set will be sorted automatically according to their natural ordering.
1 |
TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); |
The above statement will create a new TreeSet object whose elements will be of type Integer and will be sorted automatically in ascending order i.e. natural order for the type Integer.
1 2 3 4 5 6 7 |
TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); tSetNumbers.add(3); tSetNumbers.add(2); tSetNumbers.add(1); System.out.println(tSetNumbers); |
Output
1 |
[1, 2, 3] |
2. Create a new TreeSet object with custom Comparator
The default constructor creates a set object whose elements will be sorted by the natural ordering. If you do not want the elements natural ordering, you can specify a custom Comparator object using the below given constructor.
1 |
public TreeSet(Comparator<? super E> comparator) |
This constructor creates a new and empty TreeSet object whose elements will be sorted according to the custom comparator.
The below given example shows how to sort TreeSet Integer object elements in descending orders using a comparator.
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.Comparator; import java.util.TreeSet; /* * This comparator will sort the Integer objects in * descending order */ class DescIntegerComparator implements Comparator<Integer>{ public int compare(Integer i1, Integer i2) { return i2 - i1; } } public class CreateTreeSetJavaExample { public static void main(String[] args) { /* * Specify the custom comparator in the TreeSet constructor * to change the natural ordering of the elements */ TreeSet<Integer> tSetNumbers = new TreeSet<Integer>( new DescIntegerComparator()); tSetNumbers.add(1); tSetNumbers.add(2); tSetNumbers.add(3); System.out.println(tSetNumbers); } } |
Output
1 |
[3, 2, 1] |
3. How to create new TreeSet from another Set like HashSet?
Use below given copy constructor to create a TreeSet from any existing collection object.
1 |
public TreeSet(Collection<? extends E> collection) |
This constructor creates a new TreeSet object containing all the elements of the specified collection object and sorted according to their natural order.
The below given example shows how to convert HashSet to TreeSet using this constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
HashSet<Integer> hSetNumbers = new HashSet<Integer>(); hSetNumbers.add(45); hSetNumbers.add(12); hSetNumbers.add(43); hSetNumbers.add(32); /* * To convert a HashSet to a TreeSet, use the * below given constructor */ TreeSet<Integer> tSet = new TreeSet<Integer>( hSetNumbers ); System.out.println("TreeSet contains: " + tSet); |
Output
1 |
TreeSet contains: [12, 32, 43, 45] |
As we can see from the output, all elements of the HashSet object are automatically sorted in ascending order in the TreeSet object.
Important Note: All elements of the specified collection must implement the Comparable interface for this constructor to work. If they have not, java.lang.ClassCastException: cannot be cast to java.lang.Comparable exception will be thrown.
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