Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中将2d集合转换为1d?_Java_Multidimensional Array_Collections - Fatal编程技术网

如何在java中将2d集合转换为1d?

如何在java中将2d集合转换为1d?,java,multidimensional-array,collections,Java,Multidimensional Array,Collections,有没有办法将2d集合转换为1d 输出为:[[hello,hallo],[hi]] 要求是:[你好,你好] 我的尝试: Collection<Collection<String>> st = new ArrayList<>(); Collection<String> co1 = new ArrayList<>(); Collection<String> co2 = new ArrayList<>()

有没有办法将
2d集合
转换为
1d

输出为:
[[hello,hallo],[hi]]

要求是:
[你好,你好]

我的尝试:

Collection<Collection<String>> st = new ArrayList<>();
    Collection<String> co1 = new ArrayList<>();
    Collection<String> co2 = new ArrayList<>();
    co1.add("hello");
    co1.add("hallo");
    co2.add("hi");
    st.add(co1);
    st.add(co2);
    System.out.println(st);
Collection st=new ArrayList();
集合co1=新的ArrayList();
集合co2=新的ArrayList();
co1.添加(“你好”);
co1.添加(“问候”);
二氧化碳。添加(“hi”);
st.add(co1);
标准加入量(co2);
系统输出打印LN(st);

如果您使用的是
Java8+
,您可以使用以下内容:

Collection result=st.stream()
.flatMap(集合::流)
.collect(Collectors.toList());

但请注意,我在这里不单独处理重复项。

只需将
收集器.toList()
替换为
收集器.toSet()
,这将删除所有重复项。