Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a TreeSet to ArrayList?

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 ?

like image 587
priyank Avatar asked Feb 17 '12 03:02

priyank


People also ask

How do I add all elements of an ArrayList to TreeSet?

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.

Is TreeSet an array?

The toArray() method of Java TreeSet is used to form an array of the same elements as that of the TreeSet.

Will TreeSet allow duplicates?

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.


1 Answers

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); 
like image 164
yegor256 Avatar answered Sep 23 '22 07:09

yegor256