This example shows how to convert a TreeSet to an array in Java. This example also shows how to convert a TreeSet object to an array using the toArray method and Java 8 stream.
How to convert a TreeSet object to an array in Java?
There are a couple of ways using which we can convert a TreeSet object to an array in Java as given below.
1. Using the toArray method of the TreeSet class
The toArray
method of the TreeSet class returns a new array containing all the elements of the TreeSet object.
1 |
public <T> T[] toArray(T[] a) |
The array returned by the toArray
method has elements in the order that is returned by the iterator of the TreeSet object. For a TreeSet object, that is the natural ordering of the elements (defined by the Comparable interface) or the order defined by the custom 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 32 |
import java.util.TreeSet; public class TreeSetToArrayExample { public static void main(String[] args) { TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); tSetNumbers.add(25); tSetNumbers.add(23); tSetNumbers.add(27); tSetNumbers.add(22); tSetNumbers.add(28); /* * To convert a TreeSet to an array, use the * toArray method. */ //create a new empty array of the same size Integer[] intArray = new Integer[ tSetNumbers.size() ]; //use the toArray method and pass the empty array intArray = tSetNumbers.toArray(intArray); //iterate the array for(Integer number : intArray){ System.out.println( number ); } } } |
Output
1 2 3 4 5 |
22 23 25 27 28 |
As we can see from the output, the elements of the array are sorted in the natural order of the TreeSet elements i..e ascending order for the Integer type.
Important Note:
If the array specified in the toArray
method is large enough to fit all the TreeSet elements, the same array is filled with the TreeSet elements and returned. If the specified array is bigger than the size of the TreeSet, the array element that immediately comes after the TreeSet elements is set to null.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); tSetNumbers.add(25); tSetNumbers.add(23); tSetNumbers.add(27); tSetNumbers.add(22); tSetNumbers.add(28); Integer[] intArray = new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; intArray = tSetNumbers.toArray(intArray); //iterate the array for(Integer number : intArray){ System.out.println( number ); } |
Output
1 2 3 4 5 6 7 8 9 10 |
22 23 25 27 28 null 7 8 9 10 |
As we can see from the output, the array element “6” is set to null to mark the end of the TreeSet elements and the rest of the array elements are kept as is.
If the specified array is smaller than the TreeSet size, the toArray
method allocates a new array of the same size, fills it with the TreeSet elements and returns it. Always pass the array of the same size as that of the TreeSet object to avoid the costly operation of allocating a new array.
2. Using Java 8 stream
If you are using Java version 8 or later, you can use the Java stream to convert to an array as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); tSetNumbers.add(25); tSetNumbers.add(23); tSetNumbers.add(27); tSetNumbers.add(22); tSetNumbers.add(28); //use Java 8 stream to convert TreeSet to array Integer[] intArray = tSetNumbers.stream().toArray(Integer[]::new); for(Integer number : intArray){ System.out.println( number ); } |
Output
1 2 3 4 5 |
22 23 25 27 28 |
Please also visit how to convert an array to TreeSet 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