IntStream distinct() method in Java

Java 8Object Oriented ProgrammingProgramming

The distinct() method in the IntStream class in Java returns a stream consisting of the distinct elements of this stream.

The syntax is as follows

IntStream distinct()

Let’s say we have the following elements in the stream. Some of them are repeated

IntStream intStream = IntStream.of(10, 20, 30, 20, 10, 50, 80, 90, 100, 80);

To get the distinct elements, use the IntStream distinct() method.

The following is an example to implement IntStream distinct() method in Java

Example

 Live Demo

import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(10, 20, 30, 20, 10, 50, 80, 90, 100, 80);
      System.out.println("Displaying the distinct elements:");
      intStream.distinct().forEach(System.out::println);
   }
}

Output

Displaying the distinct elements:
10
20
30
50
80
90
100
raja
Published on 28-Mar-2019 07:52:27
Advertisements