简体   繁体   English

在 Java 中将数据从 HashSet 移动到 ArrayList

[英]Moving data from a HashSet to ArrayList in Java

I have the following Set in Java:我在 Java 中有以下Set

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

and I want to move its data to an ArrayList :我想将其数据移动到ArrayList

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

Is it possible to do that?有可能这样做吗?

Moving Data HashSet to ArrayList将数据HashSet移动到ArrayList

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

Here usrAllTemp is an ArrayList , which has some values.这里usrAllTemp是一个ArrayList ,它有一些值。 Same Way usrAll(ArrayList) getting values from userAllSet(HashSet) . usrAll(ArrayList)userAllSet(HashSet)获取值的方式相同。

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.注意:您应该以小型大写字母开头变量名以遵循 Java 约定。

You can use the Stream and collector to do this, I hope it is the easiest way您可以使用 Stream 和收集器来执行此操作,我希望这是最简单的方法

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

You can use addAll() :您可以使用addAll()

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

I have the following Set in Java:我在Java中具有以下Set

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

and I want to move its data to an ArrayList :我想将其数据移动到ArrayList

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

Is it possible to do that ?有可能这样做吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM