Sobes.tech
Junior — Middle

Can you name the main types of operations performed when working with Stream in Java?

sobes.tech AI

Answer from AI

The main types of operations when working with Streams in Java include:

  • Creating Streams: from collections, arrays, generators.
  • Intermediate operations: filtering (filter), transforming (map), sorting (sorted), limiting (limit), skipping elements (skip).
  • Terminal operations: collecting results (collect), counting elements (count), searching (findFirst, anyMatch), iterating (forEach), reducing (reduce).

Example:

List<String> names = Arrays.asList("Anna", "Bob", "Charlie");
long count = names.stream()
    .filter(name -> name.startsWith("A"))
    .count();
System.out.println(count); // Will output 1