This example shows how to convert TreeSet to ArrayList (or List or LinkedList) in Java. This example also shows how to convert TreeSet to List using the constructor and addAll method.
How to convert TreeSet to ArrayList (or TreeSet to List or LinkedList) in Java?
There are a couple of ways using which we can convert TreeSet object to an object of ArrayList as given below.
1. Using ArrayList constructor
The ArrayList in Java provides a constructor that accepts a collection object.
1 |
public ArrayList(Collection<? extends E> collection) |
It creates a new ArrayList object containing all the elements of the specified collection object. Since the Collection interface is a parent interface of the Set interface, we can pass the TreeSet object in this constructor.
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 |
import java.util.ArrayList; import java.util.TreeSet; public class ArrayListToTreeSetExample { public static void main(String[] args) { TreeSet<String> tSetChars = new TreeSet<String>(); tSetChars.add("x"); tSetChars.add("d"); tSetChars.add("a"); tSetChars.add("u"); tSetChars.add("y"); /* * To convert TreeSet to ArrayList, use * the ArrayList constructor and pass the * TreeSet object */ List<String> aListChars = new ArrayList<String>( tSetChars ); for(String element : aListChars){ System.out.println( element ); } } } |
Output
1 2 3 4 5 |
a d u x y |
As we can see from the output, the TreeSet class automatically sorts the elements according to the element’s natural order. So when we converted the TreeSet to an ArrayList object, we got the string elements sorted alphabetically (i.e. natural order of the String type).
2. Using the ArrayList addAll method
Instead of using the copy constructor given above, we can also use the addAll
method of the ArrayList class.
1 |
public boolean addAll(Collection<? extends E> collection) |
The addAll
method adds all elements of the specified collection object to this List object. Just like the constructor, the addAll
method also accepts the TreeSet object as a collection object.
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 |
import java.util.ArrayList; import java.util.TreeSet; public class ArrayListToTreeSetExample { public static void main(String[] args) { TreeSet<String> tSetChars = new TreeSet<String>(); tSetChars.add("x"); tSetChars.add("d"); tSetChars.add("a"); tSetChars.add("u"); tSetChars.add("y"); //step 1 - create new empty ArrayList object List<String> aListChars = new ArrayList<String>(); //step 2 - add all elements of TreeSet to ArrayList using addAll method aListChars.addAll(tSetChars); for(String element : aListChars){ System.out.println( element ); } } } |
Output
1 2 3 4 5 |
a d u x y |
As opposed to the copy constructor approach, this approach is a two-step process, first, we need to create an empty List object and then we need to add all elements of the TreeSet object to the List object using the addAll
method.
We can also convert TreeSet to LinkedList in Java using any of the above given approaches. We just need to replace the ArrayList constructor with the LinkedList constructor.
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