Class TeeingCollectorExamples

java.lang.Object
net.jrodolfo.java_evolution.java12.TeeingCollectorExamples

public class TeeingCollectorExamples extends Object
Demonstrates Collectors.teeing(java.util.stream.Collector, java.util.stream.Collector, java.util.function.BiFunction), introduced in Java 12.

Before Java 12, computing two independent summaries from one stream often required two stream passes or a custom collector. That made simple aggregate combinations more complex than necessary.

teeing solves this by sending the same input into two downstream collectors and combining their results with a final function.

  • Constructor Details

    • TeeingCollectorExamples

      public TeeingCollectorExamples()
  • Method Details

    • range

      public int range(List<Integer> numbers)
      Computes minimum and maximum values in one stream pass.
      Parameters:
      numbers - the numbers to summarize
      Returns:
      the range between the minimum and maximum values
    • summarize

      public TeeingCollectorExamples.NumberSummary summarize(List<Integer> numbers)
      Computes a count and average in one stream pass.
      Parameters:
      numbers - the numbers to summarize
      Returns:
      a simple summary object