Senior
What types of operations are available in streams (Stream API)?
sobes.tech AI
Answer from AI
The Stream API in Java supports several types of operations, which can be divided into intermediate and terminal operations.
Intermediate operations return a new Stream and allow chaining of data processing:
filter— filtering elements based on a conditionmap— transforming elementsflatMap— transforming and "flattening" nested structuresdistinct— removing duplicatessorted— sorting elementslimit— limiting the number of elementsskip— skipping the first N elements
Terminal operations complete the processing and return a result:
forEach— performing an action for each elementcollect— collecting elements into a collection or other structurereduce— reducing elements into a single valuecount— counting the number of elementsanyMatch,allMatch,noneMatch— checking conditionsfindFirst,findAny— finding elements
Intermediate operations are lazy and are only executed when a terminal operation is invoked.