Java流映射结果为reduce

时间:2018-04-11 05:34:39

标签: java java-stream reduce

我们说我有一个字符串集合

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

我希望通过stream api

获取字符串组合的集合
["a", "ab", "abc"]

我的解决方案是以这种方式使用reduce

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

在这种情况下,我得到了物品集合中我需要的东西:["a", "ab", "abc"]。 有没有更好的方法来获得标准流api?

0 个答案:

没有答案