Java 简明教程

Java - Teeing Collectors

Java Collectors.teeing() Method

Java 12 引入了一个 new static 方法 Collectors interface,它可以在 collection 上执行两个不同的操作,然后合并结果。

Syntax

teeing 方法的语法如下 −

public static Collector<T, ?, R> teeing(
   Collector<? super T, ?, R1> downstream1,
   Collector<? super T, ?, R2> downstream2,
   BiFunction<? super R1, ? super R2, R> merger
)

此处,传递给 teeing 收集器的集合的每个元素都由 downstream1 和 downstream2 收集器处理,一旦这两个收集器完成处理,结果就会传递给 BiFunction 收集器,以合并结果或相应处理。它类似于对某个集合调用两个函数,然后调用第三个函数来处理前两个函数的结果。

此处,我们对某个集合执行不同的函数,然后使用合并器 BiFunction 合并结果。

Example - Using teeing collectors to get mean of n numbers

在这个示例中,我们获取 downstream1 收集器中的数字总和、downstream2 收集器中的数字计数,然后在合并函数中计算平均值。当我们获得一大串数字并且流大小不可用时,这很有用。

package com.tutorialspoint;

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Tester {
   public static void main(String[] args) {
      double mean
         = Stream.of(1, 2, 3, 4, 5, 6, 7)
            .collect(Collectors.teeing(
               Collectors.summingDouble(i -> i), Collectors.counting(),
               (sum, n) -> sum / n));

      System.out.println(mean);
   }
}

Output

让我们编译并运行上述程序,这将生成以下结果 −

4.0

Example - Using teeing collectors to get lowest and highest marks of student objects

在此示例中,我们在 downstream1 收集器中获取学生最低分数,在 downstream2 收集器中获取学生最高分数。然后,使用合并函数,我们创建一个哈希表,其条目包含最低和最高分数的学生。

package com.tutorialspoint;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

public class Tester {

   public static void main(String args[]) {
      // list of students
      List<Student> students = Arrays.asList(
         new Student(1, "Robert", 390),
         new Student(2, "Julie", 410),
         new Student(3, "John", 440),
         new Student(4, "Michael", 420));

      // collect the result in hashmap
      HashMap<String, Student> result = students.stream().collect(
      // apply the teeing operator
      Collectors.teeing(
         // get the student having highest marks
         Collectors.maxBy(Comparator.comparing(Student::getMarks)),
         // get the student having lowest marks
         Collectors.minBy(Comparator.comparing(Student::getMarks)),
         // put both student entries in the map using merger
         (s1, s2) -> {
            HashMap<String, Student> map = new HashMap<>();
            map.put("Highest", s1.get());
            map.put("Lowest", s2.get());
            return map;
         }
      ));
      System.out.println(result);
   }
}

class Student {
   int rollNo;
   String name;
   int marks;

   public Student(int rollNo, String name, int marks) {
      this.rollNo = rollNo;
      this.name = name;
      this.marks = marks;
   }

   @Override
   public String toString() {
      return "Student [RollNo=" + rollNo + ", Name=" + name + ", Marks=" + marks + "]";
   }

   public int getRollNo() {
      return rollNo;
   }

   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public int getMarks() {
      return marks;
   }

   public void setMarks(int marks) {
      this.marks = marks;
   }
}

Output

让我们编译并运行上述程序,这将生成以下结果 −

{Lowest=Student [RollNo=1, Name=Robert, Marks=390], Highest=Student [RollNo=3, Name=John, Marks=440]}