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:
- collect() — gathers stream elements into a collection or other container.
- forEach() — performs an action for each element of the stream.
- 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);