Junior — Middle
What are the main components that make up the Stream concept?
sobes.tech AI
Answer from AI
The concept of Stream in Java consists of several main components:
-
Source — a collection or other data set from which the stream is created (e.g., List, array).
-
Intermediate operations — operations that transform the stream, such as
filter(),map(),sorted(). They are lazy, meaning they are executed only when necessary. -
Terminal operations — operations that conclude the stream processing and return a result or side effect, such as
collect(),forEach(),reduce(). -
Pipeline — a chain of intermediate operations ending with a terminal operation.
Example:
List<String> names = Arrays.asList("Anna", "Bob", "Charlie");
List<String> filtered = names.stream()
.filter(name -> name.startsWith("A"))
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(filtered); // [ANNA]
Thus, Stream allows convenient and efficient processing of data sequences in a functional style.