I am using this code to convert a Set
to a List
:
Map<String, List> mainMap = new HashMap<String, List>();
for(int i=0; i<something.size(); i++){
Set set = getSet(...); //returns different result each time
List listOfNames = new ArrayList(set);
mainMap.put(differentKeyName,listOfNames);
}
I want to avoid creating a new list in each iteration of the loop. Is that possible?
Use constructor to convert it:
I wanted a very quick way to convert my set to List and return it, so in one line I did
I found this working fine and useful to create a List from a Set.
Also from Guava Collect library, you can use
newArrayList(Collection)
:This would be very similar to the previous answer from amit, except that you do not need to declare (or instanciate) any
list
object.I would do :
Recently I found this: