Sobes.tech
Senior

Name three examples of terminal operations in the Stream API.

sobes.tech AI

Answer from AI

In Java Stream API, terminal operations are operations that conclude the stream processing and return a result or a side effect. Three examples of terminal operations:

  1. collect() — gathers stream elements into a collection or another container.
List<String> list = stream.collect(Collectors.toList());
  1. forEach() — performs an action for each element of the stream.
stream.forEach(System.out::println);
  1. reduce() — reduces stream elements to a single value, such as summing or concatenating.
OptionalInt sum = IntStream.of(1, 2, 3).reduce(Integer::sum);

Other examples include: count(), anyMatch(), allMatch(), findFirst().