JDK12 Collectors.teeing 你真的需要了解一下

本文详细介绍了Java 12中Collectors.teeing的功能,通过实例展示了如何利用该API同时计算集合的计数和累加、最大值与最小值、瓜的总重和单个重量以及预约人员列表和预约人数。文章指出,Collectors.teeing可以方便地合并两个collector的结果,提高代码的效率和简洁性。
摘要由CSDN通过智能技术生成

前言

在 Java 12 里面有个非常好用但在官方 JEP 没有公布的功能,因为它只是 Collector 中的一个小改动,它的作用是 merge 两个 collector 的结果,这句话显得很抽象,老规矩,我们先来看个图(这真是一个不和谐的图????):

管道改造经常会用这个小东西,通常我们叫它「三通」,它的主要作用就是将 downstream1 和 downstream2 的流入合并,然后从 merger 流出

有了这个形象的说明我们就进入正题吧「文中代码举例比较多,更好的阅读体验点击文末——阅读原文」

Collectors.teeing

上面提到的小功能就是 Collectors.teeing API, 先来看一下 JDK 关于该 API 的说明,看着觉得难受的直接忽略,继续向下看例子就好了:

/** * Returns a {
    @code Collector} that is a composite of two downstream collectors. * Every element passed to the resulting collector is processed by both downstream * collectors, then their results are merged using the specified merge function * into the final result. * * <p>The resulting collector functions do the following: * * <ul> * <li>supplier: creates a result container that contains result containers * obtained by calling each collector's supplier * <li>accumulator: calls each collector's accumulator with its result container * and the input element * <li>combiner: calls each collector's combiner with two result containers * <li>finisher: calls each collector's finisher with its result container, * then calls the supplied merger and returns its result. * </ul> * * <p>The resulting collector is {
    @link Collector.Characteristics#UNORDERED} if both downstream * collectors are unordered and {
    @link Collector.Characteristics#CONCURRENT} if both downstream * collectors are concurrent. * * @param <T>         the type of the input elements * @param <R1>        the result type of the first collector * @param <R2>        the result type of the second collector * @param <R>         the final result type * @param downstream1 the first downstream collector * @param downstream2 the second downstream collector * @param merger      the function which merges two results into the single one * @return a {
    @code Collector} which aggregates the results of two supplied collectors. * @since 12 */public static <T, R1, R2, R>Collector<T, ?, R> teeing(Collector<? super T, ?, R1> downstream1,                          Collector<? super T, ?, R2> downstream2,                          BiFunction<? super R1, ? super R2, R> merger) {
        return teeing0(downstream1, downstream2, merger);}
<
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值