Junior — Middle
How to distinguish terminal methods from intermediate ones in programming?
sobes.tech AI
Answer from AI
In programming, terminal methods are methods that end a chain of calls and return a specific result, such as a collection, a number, or output to the screen. Intermediate methods are methods that return a modified stream of data or collection, allowing the chain of calls to continue.
For example, in Java Stream API:
- Intermediate methods:
filter(),map(),sorted()— they return a new Stream and do not perform computations immediately. - Terminal methods:
collect(),forEach(),count()— they initiate processing and return a result.
Thus, terminal methods end the chain and provide a final result, while intermediate methods transform data and enable building call chains.