I have a TreeSet which contains > 100k objects. I have another method which requires ArrayList as an param.
Is there any way I can accomplish this without iterating whole TreeSet and then adding each object manually to ArrayList ?
if we write: ArrayList al = new ArrayList(); ArrayList al2 = new ArrayList(); ArrayList al3 = new ArrayList(); TreeSet ts = new TreeSet(); ts. add(al); ts. add(al2); ts.
The toArray() method of Java TreeSet is used to form an array of the same elements as that of the TreeSet.
TreeSet cannot contain duplicate elements. The elements in a TreeSet are sorted as per their natural ordering, or based on a custom Comparator that is supplied at the time of creation of the TreeSet. TreeSet cannot contain null value. TreeSet internally uses a TreeMap to store elements.
How about this:
new ArrayList<T>(set);
For Java 7 and later, this can be simplified, as type arguments <T>
can be replaced with diamond type <>
:
new ArrayList<>(set);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With