Sobes.tech
Junior — Middle

Could you name different types of methods used in data streams?

sobes.tech AI

Answer from AI

In Java, when working with data streams (Streams API), different types of methods are used, which can be conditionally divided into intermediate and terminal methods.

Intermediate methods return a new stream and allow chaining operations, for example:

  • filter(Predicate) — filtering elements based on a condition
  • map(Function) — transforming elements
  • sorted() — sorting elements
  • distinct() — removing duplicates

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

  • collect(Collector) — collecting elements into a collection or other structure
  • forEach(Consumer) — performing an action for each element
  • reduce(BinaryOperator) — folding elements into a single value
  • count() — counting the number of elements

Example of usage:

List<String> names = Arrays.asList("Anna", "Bob", "Alice", "Brian");
long count = names.stream()
    .filter(name -> name.startsWith("A"))
    .count();
System.out.println("Number of names starting with A: " + count);