Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is Collectors.teeing() in Java?

abhilash

What is the collectors class?

Collectors is a utility class that provides various implementations of reduction operations such as grouping elements, collecting elements to different collections, summarizing elements according to various criteria, etc. The different functionalities in the Collectors class are usually used as the final operations on streams.

The teeing() method

teeing() is a static method of the Collectors class that is used to return a Collector combining the results of two Collector operations. This method was introduced in Java version 12.

The teeing method is defined in the Collectors class. The Collectors class is defined in the java.util.stream package. To import the Collectors class, use the following import statement:

import java.util.stream.Collectors;

Syntax


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)

Parameters

  • Collector<? super T, ?, R1> downstream1: The first collector.
  • Collector<? super T, ?, R2> downstream2: The second collector.
  • BiFunction<? super R1,? super R2, R> merger: The merging function to combine the results of the first and the second collector.

Return value

This method returns a collector that aggregates the results of two supplied collectors.

Code

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

public class main {

    static class Person{
        String name;
        int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    public static void main(String[] args) {
        List<Person> personList = Arrays.asList(new Person("bob", 34), new Person("bob", 43),
                new Person("mary", 81), new Person("john", 12), new Person("bob", 22));

        System.out.println("list of person objects - " + personList);

        Stream<Person> personStream = personList.stream();

        Map<String, List<Person>> result = personStream.collect(Collectors.teeing(
                                Collectors.filtering(p -> p.age % 2 == 0 , Collectors.toList()),
                                Collectors.filtering(p -> p.age % 2 != 0 , Collectors.toList()),
                (res1, res2) -> {
                    Map<String, List<Person>> map = new HashMap<>();
                    map.put("EvenAgedPersons", res1);
                    map.put("OddAgedPersons", res2);
                    return map;
                }));

        System.out.println("Result of applying teeing - " + result);

    }
}
Implementation of the "teeing" method

Explanation

  • Lines 1-3: We import the relevant packages.
  • Lines 7-23: We define a Person class with name and age as the attributes of the class.
  • Lines 26-27: We create a list of the Person objects with different names and age values called personList.
  • Line 29: We print the personList.
  • Line 31: We create a stream from the personList.
  • Line 33: We apply the teeing collector on the person stream.
  • Line 34: We apply the filtering collector that allows only even-aged persons.
  • Line 35: We apply the filtering collector that allows only odd-aged persons.
  • Lines 36-41: We apply a BiFunction that takes the results from the two collectors and creates a hashmap of the results.
  • Line 38: We print the map of even- and odd-aged persons to the console.

RELATED TAGS

java
communitycreator
RELATED COURSES

View all Courses

Keep Exploring