Sobes.tech
Junior — Middle

Can you explain what a vector is and how it is used in programming?

sobes.tech AI

Answer from AI

In the Stream API, terminal and non-terminal operations differ in purpose and behavior:

  • Non-terminal (intermediate) operations — these return a new stream and allow building a chain of transformations. They are lazy, meaning they do not execute immediately but only when a terminal operation is invoked. Examples include filter(), map(), sorted(), distinct().

  • Terminal operations — conclude the chain and trigger data processing. They return a result (such as a collection, number, or void). After a terminal operation, the stream is considered closed and cannot be reused. Examples include collect(), forEach(), reduce(), count().

Example:

List<String> result = list.stream()
    .filter(s -> s.startsWith("A"))   // non-terminal
    .map(String::toUpperCase)           // non-terminal
    .collect(Collectors.toList());     // terminal

Here, filtering and transformation are not executed immediately but only when collect() is called, which runs the processing and returns the final list.