Slicing Operations in Stream
In this lesson, we will look at some of the most common slicing methods present in the Streams API.
We'll cover the following
The slicing operations are intermediate operations, and, as the name implies, they are used to slice a stream.
Now, we will look at some of the most common slicing methods present in Streams API.
1. distinct()
The first operation that we are going to discuss is distinct()
. It returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream.
So, if you have a stream of custom objects then your custom class should override equals()
and hashcode()
methods.
Let’s look at an example to understand distinct()
better. In the below example, we have a list of countries. The list can contain duplicate elements as well. We need to print all the distinct countries.
Get hands-on with 1200+ tech skills courses.