Stream.distinct() in Java Last Updated : 06 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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 : Java // 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); } } Output : The distinct elements are : 1 2 3 4 5 Example 2 : Java // 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); } } Output : The distinct elements are : Geeks for GeeksQuiz GeeksforGeeks Example 3 : Java // 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); } } Output : The count of distinct elements is : 4 Comment More infoAdvertise with us Next Article Stream In Java S Sahil_Bansall Follow Improve Article Tags : Misc Java Java - util package Java-Functions java-stream Java-Stream interface +2 More Practice Tags : JavaMisc Similar Reads Stream In Java Stream was introduced in Java 8, the Stream API is used to process collections of objects. A stream in Java is a sequence of objects that supports various methods that can be pipelined to produce the desired result. Use of Stream in JavaThe uses of Stream in Java are mentioned below:Stream API is a 7 min read Java 8 Stream Introduction to Stream, Java Intstream, Jaa Longstream , Java DoublestreamanyMatch() noneMatch() mapToLong() findAny()forEachOrdered() forEach() allMatch() filter() findFirst() flatMapToInt() mapToInt() map() peek() counting()Iterator()Generate()Skip()SummaryStatistics()Builder()Empty()Stream toArra 1 min read Convert Stream to Set in Java Below given are some methods which can be used to convert Stream to Set in Java. Method 1 : Using Collectors Stream collect() method takes elements from a stream and stores them in a collection.collect(Collector.toSet()) collects elements from a stream to a Set. Stream.collect() method can be used t 3 min read Convert a Set to Stream in Java Set interface extends Collection interface and Collection has stream() method that returns a sequential stream of the collection. Below given are some examples to understand the implementation in a better way. Example 1 : Converting Integer HashSet to Stream of Integers. Java // Java code for conver 2 min read Comparing Streams to Loops in Java A stream is an ordered pipeline of aggregate operations(filter(), map(), forEach(), and collect()) that process a (conceptually unbounded) sequence of elements. A stream pipeline consists of a source, followed by zero or more intermediate operations; and a terminal operation. An aggregate operation 7 min read 10 Ways to Create a Stream in Java The Stream API, introduced in Java 8, it is used to process collections of objects. Stream is a sequence of objects, that supports many different methods which can be pipe lined to produce the desired result. The features of Java stream are â A stream is not a data structure alternatively it takes i 9 min read Like