Sobes.tech
Senior

Name three examples of terminal terminating operations in the Stream API.

sobes.tech AI

Answer from AI

Three examples of terminal (final) operations of the Stream API in Java:

  1. collect() — gathers stream elements into a collection or other container.
  2. forEach() — performs an action for each element of the stream.
  3. reduce() — reduces stream elements to a single value, such as summing or concatenating.

Example:

List<String> list = Arrays.asList("a", "b", "c");

// collect
List<String> collected = list.stream().collect(Collectors.toList());

// forEach
list.stream().forEach(System.out::println);

// reduce
String concatenated = list.stream().reduce("", (a, b) -> a + b);
Name three examples of terminal terminating… - sobes.tech