Sobes.tech

sobes.tech AI

Answer from AI

In Java Stream API, operations are divided into two main categories: intermediate and terminal.

  • Intermediate operations return a new Stream and allow chaining of data processing. They are lazy, meaning they are not executed until a terminal operation is invoked. Examples: filter(), map(), sorted(), distinct(), limit(), skip().

  • Terminal operations conclude the chain and return a result or side effect. After their invocation, the Stream becomes unavailable. Examples: collect(), forEach(), reduce(), count(), anyMatch(), allMatch(), findFirst().

Example usage:

List<String> names = Arrays.asList("Anna", "Bob", "Charlie", "David");

List<String> filteredNames = names.stream()
    .filter(name -> name.startsWith("A"))  // intermediate operation
    .map(String::toUpperCase)               // intermediate operation
    .collect(Collectors.toList());          // terminal operation

System.out.println(filteredNames); // [ANNA]
Can you describe the main types of operations… - sobes.tech