reduce的Java流映射结果

reduce的Java流映射结果,java,java-stream,reduce,Java,Java Stream,Reduce,假设我有一组字符串 ["a", "b", "c"] 我想通过流api获得字符串组合的集合 ["a", "ab", "abc"] 我的解决方案是以这种方式使用reduce: final List<String> items = new ArrayList<>(); List.of("a", "b", "c").stream().reduce("", (s, s2) -> { items.add(s + s2);

假设我有一组字符串

["a", "b", "c"] 
我想通过流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;
                });
final List items=new ArrayList();
stream().reduce(“,(s,s2)->{
项目。添加(s+s2);
返回s+s2;
});
在这种情况下,我在items collection中得到了我需要的东西:
[“a”、“ab”、“abc”]

有没有一种更漂亮的方法可以通过标准的流api来实现这一点?

如果这段代码运行良好,那么这个问题就与堆栈溢出无关了。这可能对我们的姐妹网站有好处,但请记住在发布前检查。我认为在这种情况下使用for循环会更干净:p:
String combined=“”;对于(字符串str:collection)项,添加(组合+=str)减少不应该有任何方面effects@Lino同意。无副作用的解决方案也可以利用并行处理。不过,它看起来就不那么“漂亮了”(我的尝试最终只有10行左右)。