The Stream interface supports a filter method that takes a predicate as an argument. Below we will filter any Post that has a word length of less than 500 then converting the stream to a list.
Filter unique elements
Streams interface also supports a distinct method. Similar to a select distinct column from table it returns a stream with unique elements according to the implmementation of the equals method. This example we will find unique list of tags. Since tags is a string seperated by a comma, we first need to split, trim and convert all elements to lower case. Finally we will call distinct and convert the stream to a list.
Filter and truncate
Another method that stream supports is limit which returns a stream consisting of the elements of this stream, truncated to be no longer than the parameter maxSize in length. In our example we may only want to display the first two posts to a user possibly on a main landing page or as a side nav element.
Output
Skip elements
Limit and skip complement each other. Limit truncates a stream, skip will return a stream consisting of the remaining elements after the first n elements provided as a parameter. In the instance where the stream has fewer elements than provide n, an empty stream will be returned. Using posts we will skip the first 4 elements and return the 5th element in the stream.