Javaで1Dに2Dコレクションを変換する方法?

アレックス:

変換する何らかの方法がある2d Collectionには1d

出力は次のようになります。 [[hello, hallo], [hi]]

requieredは以下のとおりです。 [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);
エリトリア:

:あなたがJava8 +を使用している場合は、のようなものを使用することができます

Collection<String> result = st.stream().flatMap(Collection::stream).collect(Collectors.toList());

しかし、私は別にここに重複を扱わないことに注意してください。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=364851&siteId=1