Class StreamGatherersExamples

java.lang.Object
net.jrodolfo.java_evolution.java24.StreamGatherersExamples

public class StreamGatherersExamples extends Object
Demonstrates Stream Gatherers, finalized in Java 24.

Before gatherers, custom intermediate stream operations were difficult to express. Simple operations such as map and filter work on one element at a time, while a terminal collect operation runs only after the stream has reached the end. That left a gap for operations that need to emit values while still flowing through the pipeline. Developers often had to leave the stream pipeline or force the problem into a terminal collector. Gatherers fill that gap by letting streams model operations such as fixed windows and running scans as intermediate operations.

A gatherer is useful when each emitted output may depend on more than one input element, or on state accumulated while the stream is flowing. That is why windowing and scanning are good teaching examples.

  • Constructor Details

    • StreamGatherersExamples

      public StreamGatherersExamples()
  • Method Details

    • fixedWindows

      public List<List<Integer>> fixedWindows(List<Integer> values, int size)
      Groups values into fixed-size windows. Windowing is an intermediate operation because gather(...) changes how elements flow through the pipeline before the final toList() terminal operation runs.
      Parameters:
      values - values to window
      size - window size
      Returns:
      fixed-size windows, with a smaller final window when needed
    • runningSum

      public List<Integer> runningSum(List<Integer> values)
      Produces a running sum. Scanning is stateful: each output depends on the accumulated state from earlier input values.
      Parameters:
      values - values to scan
      Returns:
      running sum after each value