Stream.distinct() in Java

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.

Syntax :

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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Implementation of Stream.distinct()
// to get the distinct elements in the List
import java.util.*;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a list of integers
        List<Integer> list = Arrays.asList(1, 1, 2, 3, 3, 4, 5, 5);
  
        System.out.println("The distinct elements are :");
  
        // Displaying the distinct elements in the list
        // using Stream.distinct() method
        list.stream().distinct().forEach(System.out::println);
    }
}

chevron_right


Output :

The distinct elements are :
1
2
3
4
5

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Implementation of Stream.distinct()
// to get the distinct elements in the List
import java.util.*;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a list of strings
        List<String> list = Arrays.asList("Geeks", "for", "Geeks",
                                          "GeeksQuiz", "for", "GeeksforGeeks");
  
        System.out.println("The distinct elements are :");
  
        // Displaying the distinct elements in the list
        // using Stream.distinct() method
        list.stream().distinct().forEach(System.out::println);
    }
}

chevron_right


Output :

The distinct elements are :
Geeks
for
GeeksQuiz
GeeksforGeeks

Example 3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Implementation of Stream.distinct()
// to get the count of distinct elements
// in the List
import java.util.*;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a list of strings
        List<String> list = Arrays.asList("Geeks", "for", "Geeks",
                                          "GeeksQuiz", "for", "GeeksforGeeks");
  
        // Storing the count of distinct elements
        // in the list using Stream.distinct() method
        long Count = list.stream().distinct().count();
  
        // Displaying the count of distinct elements
        System.out.println("The count of distinct elements is : " + Count);
    }
}

chevron_right


Output :

The count of distinct elements is : 4


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.