Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Streams and Functional

Collectors.teeing() method

Reference for Java Collectors.teeing() (Java 12): feed a stream to two collectors in a single pass and merge their results with a BiFunction.

Collectors.teeing(c1, c2, merger) sends every element of a stream to two collectors at the same time and then combines their two results with a merge function. It was added in Java 12. The name comes from the plumbing "tee" fitting that splits one pipe into two.

A stream can only be consumed once, so before teeing() you had to either collect it into a list first or write a custom collector to compute two things from the same data, such as a minimum and a maximum, or a sum and a count.

Java
var result = stream.collect(Collectors.teeing(
        Collectors.counting(),
        Collectors.summingInt(x -> x),
        (count, sum) -> new Stats(count, sum)));

Try it

Java Min and max in one pass

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Partition-like results and nesting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
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)
Name Description
downstream1 The first collector. Every element is given to it.
downstream2 The second collector. Every element is given to it as well.
merger Called once at the end with both results; its return value is the final result.

Returns: a collector whose result is whatever the merger returns. Since Java 12.

Usage notes

  • Single traversal. The source is read once and each element goes to both collectors, which matters for one-shot sources such as Files.lines() or a stream of generated values.
  • Order of arguments is the order of the merger's parameters: the result of downstream1 comes first.
  • Both collectors see all elements. If you want to split elements between two groups, use partitioningBy() or put filtering() in front of each collector as in the second example.
  • Empty input: both collectors still produce their empty results (for example Optional.empty() from minBy), and the merger is still called.
  • Merger exceptions (such as Optional.get() on an empty result) propagate out of collect().
  • Nesting works, which lets you compute more than two values, though a small record plus a custom collector may read better beyond three.
  • Parallel streams: both downstream collectors must be safe to combine; all standard ones are.