Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most concise way to convert a Set<T> to a List<T>

For example, I am currently doing this:

Set<String> setOfTopicAuthors = ....

List<String> list = Arrays.asList( 
    setOfTopicAuthors.toArray( new String[0] ) );

Can you beat this ?

like image 808
Jacques René Mesrine Avatar asked Sep 30 '22 08:09

Jacques René Mesrine


People also ask

Can we cast a set to a list?

Typecasting to list can be done by simply using list(set_name) . Using sorted() function will convert the set into list in a defined order. The only drawback of this method is that the elements of the set need to be sortable.

Can we convert list string to set?

Given a list (ArrayList or LinkedList), convert it into a set (HashSet or TreeSet) of strings in Java. We simply create an list. We traverse the given set and one by one add elements to the list.


1 Answers

List<String> list = new ArrayList<String>(listOfTopicAuthors);
like image 448
Schildmeijer Avatar answered Oct 07 '22 01:10

Schildmeijer