Moving data from a HashSet to ArrayList in Java

心已入冬 提交于 2019-12-04 16:20:04

问题


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 ?


回答1:


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.




回答2:


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




回答3:


You can use addAll():

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



回答4:


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

listname = setName.stream().collect(Collectors.toList());


来源:https://stackoverflow.com/questions/13632135/moving-data-from-a-hashset-to-arraylist-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!