
list : list = Stream.of(1,2,3).collect(Collectors.toList()); // 1, 2 3 Collectors#teeing . According to official documentation:“... returns a collector made up of two downstream collectors. Each element transferred to the resultant collector is processed by both downstream collectors, and then their results are combined using a special function that connects them to the final result. "Method Header:Original"It is a combination of two downstream collectors. It’s a process that has been used for 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 derived from tee. According to Wikipedia, “tee is the most common fitting (connecting part of a pipeline, approx. Translator) used to unite [or separate] the flow of liquids (in this case they mean stream, stream - stream / stream, approx. Translator)”.Other names have been suggested: bisecting, duplexing, bifurcate (split), replicator, fanout, tapping, unzipping, collectionToBothAndThen, biCollecting, expanding (expanding), forking, etc.
All alternatives priced by Core developers can be found here .
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( // , , Collectors.filtering(Guest::isParticipating, // Collectors.mapping(o -> o.name, Collectors.toList())), // , Collectors.summingInt(Guest::getParticipantsNumber), // , // EventParticipation::new )); System.out.println(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( // Collectors.filtering(n -> n.contains("xx"), Collectors.toList()), // Collectors.filtering(n -> n.endsWith("One"), Collectors.toList()), // - (List<String> list1, List<String> list2) -> List.of(list1, list2) )); System.out.println(result); // -> [[Devoxx, Voxxed Days], [Code One, Basel One]] Teeing , and you can simply use AverageInt and a simple collector.Teeing to return two values: var result = Stream.of(5, 12, 19, 21) .collect(Collectors.teeing( // Collectors.counting(), // Collectors.summingInt(n -> Integer.valueOf(n.toString())), // : (count, sum) -> new Result(count, sum); Result::new )); System.out.println(result); // -> {count=4, sum=57} 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 to store the result of BiFunction . Please do not do this, because you cannot store the last argument in the Map . There is no standard object for storing two values in Java Core — you will need to create it yourself.Source: https://habr.com/ru/post/445622/
All Articles