Javatpoint Logo
Javatpoint Logo

Java 8 Stream.distinct() Method

The distinct() operation in Java 8 is mostly related to streams and is employed to remove duplicate elements from the stream. The Stream API, which was unveiled in Java 8 to offer a more practical method of interacting with collections, includes this action.


Purpose of distinct() Method

The distinct() operation makes sure that each element only appears once in the outcome by removing duplicate components from a stream.

Application of distinct() Method

1. Simple kinds

separate compares elements based on their equality or natural order when dealing with simple kinds (such as integers and strings).

2. Objects

To determine equality for custom objects, the distinct operation uses the equals method of the object.

Working of distinct() Method

  1. The unique operation is an intermediate stateful activity. To record the unique components that have been encountered thus far during stream processing, it keeps track of them in a set (or other data structure).
  2. The distinct() operation determines if each element is already in the set as it passes through the stream. If so, the element is filtered out and regarded as a duplicate.

Let's examine the different ways that Java 8's unique operation can be used, as well as full code samples for each case.

1. Using distnict() Method With Java Stream

In Java Stream, duplicates can be eliminated from a stream of elements by using the distinct() method.


List: [1, 2, 3, 4, 2, 3, 5, 6, 7, 8, 1]
Distinct elements of the list: [1, 2, 3, 4, 5, 6, 7, 8] 


We generate an integer (number) list that has some duplicate entries. To create a stream from the list, we utilize the stream() method. To remove duplicates, the stream is subjected to the distinct() process. Lastly, we print both the original and distinct lists after the collect(Collectors.toList()) function gathers the stream elements into a new list (distinctNumbers).

2. Using distinct() Method with Objects

Use distinct() together with a custom comparator if we have a list of custom objects and want to remove duplicates based on a specific property.


Original List: [Person@6956de9, Person@769c9116, Person@6aceb1a5, Person@2d6d8735]
Distinct List: [Person@6956de9, Person@769c9116, Person@6aceb1a5, Person@2d6d8735]


Based on the name and age data, we get a list of Person objects with some duplicate entries. The list is transformed into a stream of Person objects using the stream() function. Using object equality as the default equality check, duplicates are removed using the distinct() function. Printing of both the original and distinct lists follows, with the outcome compiled into a new list (distinctPeople).

3. Using distinct() Method with Complex Objects

We can use distinct() in conjunction with other stream operations to handle more complicated cases.


Nested Lists: [[11, 22, 23], [22, 13, 24], [13, 34, 25]]
Distinct Numbers in Nested Lists: [11, 22, 23, 13, 24, 34, 25] 


The list of lists is transformed into a stream of lists using the stream() method. The nested lists are flattened into a single integer stream using the flatMap(List::stream) function. Duplicate number elimination is accomplished via the distinct() technique. Both the original nested lists and distinct numbers are reported when the outcome is gathered into a new list (distinctNumbers).


In summary, in a functional programming paradigm, the unique method in Java 8 streams is a useful tool for eliminating duplication and guaranteeing uniqueness. It is adaptable and suitable for different kinds of data, and its statefulness is contained within the operation, maintaining the stream pipeline's overall statelessness. Equality tests must be implemented correctly, and developers must take performance into account, particularly when working with big or complicated data sets. The unique operation turns out to be a workable way to clean, preprocess, and preserve integrity in data sets or streams.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA