java8 stream collect方法

参考自:Java Platform SE 8

Mutable reduction
A mutable reduction operation accumulates input elements into a mutable result
 container, such as a Collection or StringBuilder, as it processes the elements 
in the stream.
If we wanted to take a stream of strings and concatenate them into a single long 
string, we could achieve this with ordinary reduction:


     String concatenated = strings.reduce("", String::concat)
 
We would get the desired result, and it would even work in parallel. 
However, we might not be happy about the performance! 
Such an implementation would do a great deal of string copying, 
and the run time would be O(n^2) in the number of characters. 
A more performant approach would be to accumulate the results into a StringBuilder, 
which is a mutable container for accumulating strings. 
We can use the same technique to parallelize mutable reduction as we do with ordinary reduction.

The mutable reduction operation is called collect(), as it collects together the desired results into a result container such as a Collection. 
A collect operation requires three functions: 
a supplier function to construct new instances of the result container, 
an accumulator function to incorporate an input element into a result container, and a combining function to merge the contents of one result container into another. The form of this is very similar to the general form of ordinary reduction:


 <R> R collect(Supplier<R> supplier,
               BiConsumer<R, ? super T> accumulator,
               BiConsumer<R, R> combiner);
 
As with reduce(), a benefit of expressing collect in this abstract way is that it is directly amenable to parallelization: we can accumulate partial results in parallel and then combine them, so long as the accumulation and combining functions satisfy the appropriate requirements. For example, to collect the String representations of the elements in a stream into an ArrayList, we could write the obvious sequential for-each form:


     ArrayList<String> strings = new ArrayList<>();
     for (T element : stream) {
         strings.add(element.toString());
     }
 
Or we could use a parallelizable collect form:

     ArrayList<String> strings = stream.collect(() -> new ArrayList<>(),
                                                (c, e) -> c.add(e.toString()),
                                                (c1, c2) -> c1.addAll(c2));
 
or, pulling the mapping operation out of the accumulator function, we could express it more succinctly as:

     List<String> strings = stream.map(Object::toString)
                                  .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
 
Here, our supplier is just the ArrayList constructor, the accumulator adds the stringified element to an ArrayList, and the combiner simply uses addAll to copy the strings from one container into the other.

其中第一个参数为supplier构建一个新的返回值的实例

第二个参数为将下一个元素放入返回值中

第三个参数为合并返回值的集合

a supplier function to construct new instances of the result container, 
an accumulator function to incorporate an input element into a result container, 
and a combining function to merge the contents of one result container into another. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值