Introduction: This tutorial explains how to do filtering and slicing in Java 8 Streams using the filter, distinct, limit and skip methods. It assumes that you are familiar with basics of Java 8 Streams APITutorial on Basics of Java 8 Streams API.
Streams Filtering & Slicing Basics: Java 8 Streams support declarative filtering out of elements along with the ability to slice-off portions of a list. Streams support four operations to achieve this –
skip(n). Lets quickly look at what these methods do followed by a java example which uses all of these.
Methods in Streams API for filtering and slicing
- Filter method: The filter method filters out elements from a given stream. It uses a Predicate function
Click to Read Tutorial on Predicate Function instance which it applies to the whole stream and returns a filtered stream containing those elements which match the Predicate.
Syntax of filter method:
- Distinct Method: The distinct method when applied to a stream, returns a stream instance which has all elements unique/distinct. I.e. every unique element is present only once in the resultant stream. The uniqueness of elements in the resultant stream is determined by the equals & hashcode implementation in these elements.
Syntax of distinct method:
- Limit(n) method: The limit method puts an upper-limit cap purely based on the number of elements in the stream. A limit of n applied to a stream returns a stream which contains exactly n elements if the original stream had more than or equal to n elements. In case the original stream had less than n elements, and limit of n has been applied on the stream, then there is no affect on the stream elements, i.e. the returned stream is same as the original stream.
Syntax of limit method:
java.util.Streamof n elements
- Skip(n) method: The skip method is complimentary to the limit method. I.e. the skip method returns a truncated version of the original stream such that the first n elements in the list are skipped and the remaining elements are returned in the resulting stream.
Syntax of skip method:
java.util.Streamof elements post first n elements
The reason limit(n) & skip(n) are complimentary is because given a stream of size k, streams obtained from
skip(n) applied on the original stream (where,
n<=k) when aggregated together give us the original stream back. While
limit(n) gives the first
n elements from positions
skip(n) gives elements from positions
k. Thus, joining the streams obtained from
skip(n) gives us the original stream back.
Java example demonstrating the use of filter, distinct, limit & skip methods
- 2 classes are defined in the code above. First is
Employeeclass which consists of 2 attributes -
age. It also contains
- Second class is
FilteringSlicingStreamswhich defines a list of 9 Employees using
- In the
main()method first the
filter()method is called with
Predicatelambda defined in such a way that only employees greater than 24 years of age satisfy the condition. As a result the
filteredListvariable gets a list of Employees with 6 employees in them. I have highlighted the different portions of output with green color for easy readability. There are 6 employee records printed in the output below the line "After applying filter method".
- Next the
filteredListis taken and
distinct()method is applied to it. This list of 6 filtered employees is now reduced to 5 with 1 instance of Harry Major aged 25 being removed as it is duplicate. Note that
equals()Read tutorial explaining why hashcode & equals are overridden implementation in
Employeehas been done to make distinct method work correctly. The list of 5 Employees remaining as a result is printed in the output below the line "After applying distinct() method".
- Next the
limit(2)method is applied to the
filteredListinstance returned after applying distinct. This limits the resultant list to exactly 2
Employeerecords which are shown in the output below the line "After applying limit(2) method".
- Lastly the
skip(1)method is applied to the
filteredListwhich now contains 2 employees. This causes the resultant stream and list to skip the first
Employeein the list(Tom Jones) and we are now left with only 1
Employeein the list(Harry Major). Details of this 1 employee are printed below the output line "After applying skip(1) method".
Summary: This wraps-up the tutorial on filtering and slicing of Streams using the filter, distinct, limit & skip methods.