繁体   English   中英

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

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

我在 Java 中有以下Set

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

我想将其数据移动到ArrayList

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

有可能这样做吗?

将数据HashSet移动到ArrayList

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

这里usrAllTemp是一个ArrayList ,它有一些值。 usrAll(ArrayList)userAllSet(HashSet)获取值的方式相同。

你只需要循环:

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

注意:您应该以小型大写字母开头变量名以遵循 Java 约定。

您可以使用 Stream 和收集器来执行此操作,我希望这是最简单的方法

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

您可以使用addAll()

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

我在Java中具有以下Set

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

我想将其数据移动到ArrayList

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

有可能这样做吗?

暂无
暂无

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

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