Migrating data from a HashSet to an ArrayList in Java

I have the following Set in Java:

 Set< Set<String> > SetTemp = new HashSet< Set<String> >(); 

and I want to move its data to an ArrayList :

 ArrayList< ArrayList< String > > List = new ArrayList< ArrayList< String > >); 

Can this be done?

+14
java arraylist list data-structures hashset
source share
4 answers

You just need to execute the loop:

 Set<Set<String>> setTemp = new HashSet<Set<String>> (); List<List<String>> list = new ArrayList<List<String>> (); for (Set<String> subset : setTemp) { list.add(new ArrayList<String> (subset)); } 

Note: you must start the variable names with small caps in order to follow Java conventions.

+13
source share

Moving HashSet Data to ArrayList

 Set<String> userAllSet = new HashSet<String>(usrAllTemp); List<String> usrAll = new ArrayList<String>(userAllSet); 

Here usrAllTemp is an ArrayList that has some meanings. The same usrAll(ArrayList) path gets values ​​from userAllSet(HashSet) .

+39
source share

You can use addAll() :

 Set<String> gamesInstalledTemp = new HashSet< Set<String> >(); List<String> gamesInstalled = new ArrayList<>(); gamesInstalled.addAll(gamesInstalledTemp); 
+1
source share

You can use thread and collector to do this, I hope this is the easiest way

 listname = setName.stream().collect(Collectors.toList()); 
0
source share

All Articles