23

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 > >);

Is it possible to do that ?

durron597
  • 31,968
  • 17
  • 99
  • 158
Ghadeer
  • 608
  • 2
  • 7
  • 21

4 Answers4

71

Moving Data HashSet to ArrayList

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

Here usrAllTemp is an ArrayList, which has some values. Same Way usrAll(ArrayList) getting values from userAllSet(HashSet).

Roman
  • 4,922
  • 3
  • 22
  • 31
abhishek ringsia
  • 1,970
  • 2
  • 20
  • 28
16

You simply need to 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 should start variable names in small caps to follow Java conventions.

assylias
  • 321,522
  • 82
  • 660
  • 783
4

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

listname = setName.stream().collect(Collectors.toList());
saif
  • 1,182
  • 14
  • 27
1

You can use addAll():

Set<String> gamesInstalledTemp = new HashSet< Set<String> >();
List<String> gamesInstalled = new ArrayList<>();
gamesInstalled.addAll(gamesInstalledTemp);
DrMorteza
  • 2,067
  • 2
  • 21
  • 29