distinct() returns a stream consisting of distinct elements in a stream. distinct() is the method of Stream interface. This method uses hashCode() and equals() methods to get distinct elements. In case of ordered streams, the selection of distinct elements is stable. But, in case of unordered streams, the selection of distinct elements is not necessarily stable and can change. distinct() performs stateful intermediate operation i.e, it maintains some state internally to accomplish the operation.
Stream<T> distinct() Where, Stream is an interface and the function returns a stream consisting of the distinct elements.
Below given are some examples to understand the implementation of the function in a better way.
Example 1 :
The distinct elements are : 1 2 3 4 5
Example 2 :
The distinct elements are : Geeks for GeeksQuiz GeeksforGeeks
Example 3 :
The count of distinct elements is : 4
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.concurrent.Phaser class in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java lang.Long.byteValue() method in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.