Java Streams Api - Aggregate operations

Differences Between Aggregate Operations and Iterators

Aggregate operations, like forEach, appear to be like iterators. However, they have several fundamental differences:

  1. They use internal iteration: Aggregate operations do not contain a method like next to instruct them to process the next element of the collection. With internal delegation, your application determines what collection it iterates, but the JDK determines how to iterate the collection. With external iteration, your application determines both what collection it iterates and how it iterates it. However, external iteration can only iterate over the elements of a collection sequentially. Internal iteration does not have this limitation. It can more easily take advantage of parallel computing, which involves dividing a problem into subproblems, solving those problems simultaneously, and then combining the results of the solutions to the subproblems. See the section Parallelism (https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html) for more information.

  2. They process elements from a stream: Aggregate operations process elements from a stream, not directly from a collection. Consequently, they are also called stream operations.

  3. They support behavior as parameters: You can specify lambda expressions as parameters for most aggregate operations. This enables you to customize the behavior of a particular aggregate operation.

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {

        int start = 1;
        int end = 100;

        List<Integer> range = IntStream.rangeClosed(start, end)
                .boxed().collect(Collectors.toList());

        printTheNameOfEachPerson_AggregateOperations(range);
    }

    public static void printTheNameOfEachPerson_AggregateOperations(List<Integer> numbersList) {
        System.out.println("First list:");
        for (int number : numbersList) {
            System.out.println(number);
        }

        System.out.println("");
        System.out.println("Second list:");

        // using the aggregate operation forEach
        numbersList.stream().forEach(n -> System.out.println(n));
    }
}

Links to this note