Java 12:Teeing Collector

在本文中,我们将介绍一个在Java 12中引入的新集合。这个新功能并未在官方JEP中公布,因为它是一个标题 Create Collector的微小更改请求 ,它合并了其他两个其他collector的结果

Documentation

点击这里查看 Collectors#teeing官方文档。根据文档:
"...returns a 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."
方法签名:
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)复制代码

有趣的事实

这是一个发球台:
发球的词源可以帮助我们在日常活动中记住这个名字,或者在工作场所展示我们的知识。
Teeing起源于管道三通!根据维基百科的说法,“三通是最常见的管道配件,用于组合(或分割)流体流动。”
Linux具有拆分数据的命令tee。根据作者,我们找到了Richard Stallman。
为此功能提出的其他名称包括:二等分,双工,分叉,复制器,扇出,攻丝,解压缩,收集,扩展,分叉等。

用例示例

我收集了三个不同复杂程度的不同例子。
你可以静态引入import static java.util.stream.Collectors.*;减少编写的代码量。

嘉宾名单

我们从对象列表(流)中提取两种不同类型的信息。每位客人都必须接受邀请,才能带上家人。
我们想知道谁确认了预订 和参与者总数(包括客人和家庭成员)。
var result =  Stream.of( 
    // Guest(String name, boolean participating, Integer participantsNumber) 
    new Guest("Marco", true, 3), 
    new Guest("David", false, 2), 
    new Guest("Roger",true, 6))  
    .collect(Collectors.teeing( 
    // first collector, we select only who confirmed the participation    
    Collectors.filtering(Guest::isParticipating, 
    // whe want to collect only the first name in a list      
     Collectors.mapping(o -> o.name, Collectors.toList())), 
    // second collector, we want the total number of participants       
    Collectors.summingInt(Guest::getParticipantsNumber), 
    // we merge the collectors in a new Object, 
    // the values are implicitly passed       
    EventParticipation::new   ));  
    System.out.println(result); 
    // Result 
    // EventParticipation { guests = [Marco, Roger], 
    // total number of participants = 11 }复制代码

使用的类

class Guest { 
    private String name; 
    private boolean participating; 
    private Integer participantsNumber; 
    public Guest(String name, boolean participating, Integer participantsNumber) { 
        this.name = name; 
        this.participating = participating; 
        this.participantsNumber = participantsNumber;  
    } 
    public boolean isParticipating() { 
        return participating;  
    } 
    public Integer getParticipantsNumber() {
        return participantsNumber;  
    }
}

class EventParticipation { 
    private List<String> guestNameList; 
    private Integer totalNumberOfParticipants; 
    public EventParticipation(List<String> guestNameList, Integer totalNumberOfParticipants) { 
        this.guestNameList = guestNameList; 
        this.totalNumberOfParticipants = totalNumberOfParticipants;
    }
    @Override
    public String toString() { 
        return "EventParticipation { " + "guests = " + 
            guestNameList + ", total number of participants = " + 
            totalNumberOfParticipants + " }";  
    }
}复制代码

过滤两个不同列表中的名称

在此示例中,我们根据过滤器将名称流拆分为两个列表。
var result =  Stream.of("Devoxx", "Voxxed Days", "Code One", "Basel One", "Angular Connect")  
                .collect(Collectors.teeing( 
                // first collector  
                Collectors.filtering(n -> n.contains("xx"), Collectors.toList()), 
                // second collector  
                Collectors.filtering(n -> n.endsWith("One"), Collectors.toList()),
                // merger - automatic type inference doesn't work here  
                (List<String> list1, List<String> list2) -> List.of(list1, list2)  ));  
System.out.println(result); 
// -> [[Devoxx, Voxxed Days], [Code One, Basel One]]复制代码

计算并汇总整数流

也许,你在博客上看到了一个类似的例子,它合并了sum和count平均数。这个例子不需要Teeing,你只需要使用AverageInt和一个简单的收集器。
以下示例使用 Teeing 来返回两个值:
var result =  Stream.of(5, 12, 19, 21)    
                .collect(Collectors.teeing( 
                // first collector      
                Collectors.counting(), 
                // second collector      
                Collectors.summingInt(n -> Integer.valueOf(n.toString())), 
                // merger: (count, sum) -> new Result(count, sum);      
                Result::new  ));  
System.out.println(result); // -> {count=4, sum=57}复制代码
Result 类:
class Result { 
    private Long count; 
    private Integer sum; 
    public Result(Long count, Integer sum) { 
        this.count = count; this.sum = sum;  
    } 
    @Override 
    public String toString() { 
        return "{" + "count=" + count + ", sum=" + sum + '}';  
    }
}复制代码

陷阱

Map.Entry

许多示例用Map.Entry 存储BiFunction的结果。请不要这样做,因为你没有存储 Map。Java Core没有标准对象来存储两个值 - 你必须自己创建它。
Pair 在Apache Utils实现了 Map.Entry,由于这个原因,它不是一个有效的替代方案。

关于Java 12的一切

你可以在本 演示文稿中了解有关Java 12的更多信息和有趣的事实。
Happy collecting!
更多文章欢迎访问: http://www.apexyun.com
公众号:银河系1号
联系邮箱:[email protected]
(未经同意,请勿转载)  


猜你喜欢

转载自juejin.im/post/5c9f4536e51d455f687f18ba