New feature of jdk12: teeing collectors

brief introduction

Jdk12 is Java util. stream. Collectors added a new teeing method? Seeing that many people translate it into "tee", I can't help crying. Where is so complicated, tee is t. Its function is like a T-type. Data enters from two ends and then flows out from one end. The role of teeing is here.

talk is cheap,show me the code

My favorite thing is that a word is not code. The description of words is always a little confusing. Programmers still have to speak with programs. With a program, there is logic, with logic, everything.

Examples on major websites like maxby and minby. I don't like them here. Here's an example of counting the average and total scores of students. I hope you can like it:

    @Test
    public void useTeeing(){
        List<Student> studentList= Arrays.asList(
                new Student("alice",90),new Student("boy",20),new Student("bruce",40),new Student("batman",100)
        );
        String teeingResult=studentList.stream().collect(
                Collectors.teeing(
                        Collectors.averagingInt(Student::getscore),Collectors.summingInt(Student::getscore),(s1,s2)-> s1+ ":"+ s2
                )
        );
        log.info(teeingResult);
    }

OK, here comes the code. I built a student list above. Then through collectors The teaing operation passes in averaging int and summingint, and finally generates the final string through a merge expression.

Let's look at the output:

[main] INFO com.flydean.TeeingCollector - 62.5:250

Deep analysis of teeing method

As a programmer with pursuit, he can't sleep without an in-depth understanding of the essence of T. Let's look at the definition of T:

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)

First, analyze the return value of T method. T returns a collector. Collector is a reduction operation. It converts the input elements into a result set after accumulation.

Let's take another look at the definition of the collector interface:

public interface Collector<T,A,R> 

Collector defines three parameter types. T is the type of input element, a is the cumulative type of reduction operation, that is, the initial type of supplier, and R is the final return type. Let's draw a diagram to see the conversion relationship between these types:

A in the stream and a in the supplier are finally converted into R in finisher through accumulator and combiner.

T method needs to pass in two downstreams, which are two collectors. You can see that their return types can be different.

The last Mercer converts R1 and R2 to the final return type R.

characteristics

Finally, let's talk about characteristics, which refers to the characteristics of collectors.

Characteristics is to better perform the reduce operation of the collector.

For example, if characteristics is unordered, it means that the collector does not save the order of elements during processing, and there is no order.

If the characteristics is current, it means that the collector will handle multithreading without the stream API.

Because the first two parameters of the T method are collectors, and finally a collector is returned.

If both downstream1 and downstream2 are unordered, the last collector returned by T is also unordered.

If both downstream1 and downstream2 are current, the last collector returned by T is also current.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>