Sobes.tech
Junior — Middle

Can you list the main components included in the Java Stream API?

sobes.tech AI

Answer from AI

The main components of the Stream API in Java include:

  • Stream — a sequence of elements supporting sequential and parallel aggregate operations.
  • Intermediate operations — operations that return a new Stream, such as filter(), map(), sorted().
  • Terminal operations — operations that conclude the stream processing and return a result or side-effect, such as collect(), forEach(), reduce().
  • Collectors — utilities for collecting stream elements into collections, strings, statistics, etc.

Example of using the Stream API:

List<String> names = Arrays.asList("Anna", "Bob", "Charlie", "David");
List<String> filtered = names.stream()
    .filter(name -> name.startsWith("A"))
    .map(String::toUpperCase)
    .collect(Collectors.toList());

System.out.println(filtered); // [ANNA]