While using Imperative Programming, the developer uses the language constructs to write both what to do and how to do. Whereas while using Declarative Programming, developer has to focus only on defining what to do and the language or the framework takes care of the how to do part. Hence in Declarative Programming, the code is concise and less error-prone.
Unlike other operations we have seen till now, the
sorted
operation is stateful. It means that the operator will have to see all the elements in the stream before the result of sorting can be provided to further intermediate or terminal operator. Another example of such an operator isdistinct
In Stream
API, this is achieved using terminal operators. reduce
and collect
are the generic terminal operators provided for this purpose.