Java stream conversion result

Say I have a rowset

["a", "b", "c"] 

and I want to get a set of string combinations via stream api

["a", "ab", "abc"]

My solution is to use reduceas follows:

final List<String> items = new ArrayList<>();
List.of("a", "b", "c").stream().reduce("", (s, s2) -> {
                    items.add(s + s2);
                    return s + s2;
                });

In this case, I got what I need in the collection of elements: ["a", "ab", "abc"]. Is there a nicer way to get this with a standard api stream?

+4
source share

Source: https://habr.com/ru/post/1696045/


All Articles