Sobes.tech
Junior — Middle

Can you list the types of operations that can be performed with streams?

sobes.tech AI

Answer from AI

In Java Stream API, operations are divided into intermediate and terminal.

Intermediate operations return a new Stream and allow building a data processing pipeline:

  • filter(Predicate) — filtering elements
  • map(Function) — transforming elements
  • flatMap(Function) — transforming with unwrapping nested streams
  • distinct() — removing duplicates
  • sorted() and sorted(Comparator) — sorting
  • limit(long) — limiting the number of elements
  • skip(long) — skipping the first elements

Terminal operations complete the processing and return a result or a side effect:

  • forEach(Consumer) — performing an action for each element
  • collect(Collector) — collecting elements into a collection or other structure
  • reduce(BinaryOperator) — folding elements into a single value
  • count() — counting elements
  • anyMatch(Predicate), allMatch(Predicate), noneMatch(Predicate) — condition checks
  • findFirst(), findAny() — finding an element

Example:

List<String> names = Arrays.asList("Anna", "Bob", "Alice", "Brian");
List<String> filtered = names.stream()
    .filter(s -> s.startsWith("A"))
    .map(String::toUpperCase)
    .sorted()
    .collect(Collectors.toList());
// filtered = ["ALICE", "ANNA"]
Can you list the types of operations that can be… - sobes.tech