How to convert 2d Collection into 1d in java?

Alex :

Is there any way to convert 2d Collection into 1d?

the output is : [[hello, hallo], [hi]]

requiered is : [hello,hallo,hi]

my Try:

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);
Eritrean :

If you are using Java8+ you can use something like:

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

But note that I do not treat duplicates separately here.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360457&siteId=1