我Set
在Java中有以下内容:
Set< Set<String> > SetTemp = new HashSet< Set<String> >();
我想将其数据移动到ArrayList
:
ArrayList< ArrayList< String > > List = new ArrayList< ArrayList< String > >);
有可能这样做吗?
我Set
在Java中有以下内容:
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 中将 hashet 转换为 ararylist
方法1:我们将hashset作为参数在arraylist/list的构造函数中传递为
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
List<String> list = new ArrayList<String>(hs);
方法2:迭代Hashset并在列表中逐个添加元素
for (String str : hs) {
arraylist.add(str);
}
方法:使用 arr.addAll() 方法
With the help of addAll() you can add collection into list