This example shows how to convert an array to TreeSet in Java. This example also shows how to convert array to TreeSet using constructor, addAll method, Collections class, for loop, and Java 8 stream.
How to convert an Array to TreeSet in Java?
There are several ways using which we can convert an array to TreeSet object as given below. I am going to use an Integer array for the purpose of this example.
1. Using for loop
The simplest way is to loop through an array and add elements to the TreeSet object 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 |
import java.util.TreeSet; public class ConvertArrayToTreeSetExample { public static void main(String[] args) { Integer[] numberArray = new Integer[]{3, 2, 5, 1, 4, 2}; //create new TreeSet object of the same type TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); //use for loop for(Integer number : numberArray){ //add element to the TreeSet tSetNumbers.add(number); } System.out.println("TreeSet contains: " + tSetNumbers); } } |
Output
1 |
TreeSet contains: [1, 2, 3, 4, 5] |
2. Using TreeSet constructor
We can use the TreeSet constructor that accepts a collection object, but for that, we need to convert an array to a collection first. We can use the asList
method of the Arrays class that returns a List wrapper around an array to do that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.TreeSet; public class ConvertArrayToTreeSetExample { public static void main(String[] args) { Integer[] numberArray = new Integer[]{3, 2, 5, 1, 4, 2}; /* * Convert an array to a list and then use the * TreeSet constructor */ TreeSet<Integer> tSetNumbers = new TreeSet<Integer>( Arrays.asList(numberArray) ); System.out.println("TreeSet contains: " + tSetNumbers); } } |
Output
1 |
TreeSet contains: [1, 2, 3, 4, 5] |
3. Using the TreeSet addAll method
The addAll
method of the TreeSet class adds all elements of the specified collection object to the set object. We are going to convert array to a List using the same asList
method of the Arrays class and then use the addAll
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.TreeSet; public class ConvertArrayToTreeSetExample { public static void main(String[] args) { Integer[] numberArray = new Integer[]{3, 2, 5, 1, 4, 2}; //create new TreeSet object of the same type TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); /* * Convert array to a List and then use the addAll method */ tSetNumbers.addAll( Arrays.asList(numberArray) ); System.out.println("TreeSet contains: " + tSetNumbers); } } |
Output
1 |
TreeSet contains: [1, 2, 3, 4, 5] |
4. Using Collections addAll method
Instead of the addAll
method of the TreeSet class, we can also use the addAll
method of the Collections class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Collections; import java.util.TreeSet; public class ConvertArrayToTreeSetExample { public static void main(String[] args) { Integer[] numberArray = new Integer[]{3, 2, 5, 1, 4, 2}; //create new TreeSet object of the same type TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); /* * Use the addAll method of the Collections class * and pass the TreeSet as well as the array */ Collections.addAll(tSetNumbers, numberArray); System.out.println("TreeSet contains: " + tSetNumbers); } } |
Output
1 |
TreeSet contains: [1, 2, 3, 4, 5] |
5. Using Apache Commons CollectionUtils class
If you are using the Apache Commons library, we can use the addAll
method of the CollectionUtils class to convert an array to a TreeSet object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.TreeSet; import org.apache.commons.collections4.CollectionUtils; public class ConvertArrayToTreeSetExample { public static void main(String[] args) { Integer[] numberArray = new Integer[]{3, 2, 5, 1, 4, 2}; //create new TreeSet object of the same type TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); /* * Use the addAll method of the Apache Commons * CollectionUtils class */ CollectionUtils.addAll(tSetNumbers, numberArray); System.out.println("TreeSet contains: " + tSetNumbers); } } |
Output
1 |
TreeSet contains: [1, 2, 3, 4, 5] |
6. Using Java 8 Stream
If you are using Java version 8 or later, you can use the Java stream to convert an array to a set as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; public class ConvertArrayToTreeSetExample { public static void main(String[] args) { Integer[] numberArray = new Integer[]{3, 2, 5, 1, 4, 2}; //using Java 8 stream Set<Integer> setNumbers = Arrays.stream(numberArray).collect(Collectors.toSet()); System.out.println("TreeSet contains: " + setNumbers); } } |
Output
1 |
TreeSet contains: [1, 2, 3, 4, 5] |
As we can see from the output, the TreeSet class automatically sorts the array elements in their natural order i.e. ascending order for the type Integer. Also, the original array contained 6 elements, but when we converted it to an array, we got only 5 elements.
It is because the TreeSet is an implementation of the Set interface that does not allow duplicate elements hence the last element “2” was not added to the TreeSet as it was duplicated.
Please also visit how to convert TreeSet to an array example to know more.
This example is a part of the TreeSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeSet