Stream.map(...)和Collectors.mapping(...)之间有什么区别? [英] What's the difference between Stream.map(...) and Collectors.mapping(...)?

查看:3541
本文介绍了Stream.map(...)和Collectors.mapping(...)之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到中公开了许多功能显然是在收集器中重复的,例如Stream.map(Foo::bar)Collectors.mapping(Foo::bar, ...)Stream.count()Collectors.counting().这些方法之间有什么区别?有性能差异吗?它们是否在某种程度上影响了并行化的水平?

I've noticed many functionalities exposed in Stream are apparently duplicated in Collectors, such as Stream.map(Foo::bar) versus Collectors.mapping(Foo::bar, ...), or Stream.count() versus Collectors.counting(). What's the difference between these approaches? Is there a performance difference? Are they implemented differently in some way that affects how well they can be parallelized?

推荐答案

似乎存在与Stream中功能相同的收集器,因此它们可用作下游收集器来用作.

The collectors that appear to duplicate functionality in Stream exist so they can be used as downstream collectors for collector combinators like groupingBy().

作为一个具体示例,假设您要计算卖方的交易数".您可以这样做:

As a concrete example, suppose you want to compute "count of transactions by seller". You could do:

Map<Seller, Long> salesBySeller = 
    txns.stream()
        .collect(groupingBy(Txn::getSeller, counting()));

没有像counting()mapping()这样的收集器,这类查询将更加困难.

Without collectors like counting() or mapping(), these kinds of queries would be much more difficult.

这篇关于Stream.map(...)和Collectors.mapping(...)之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆