IntStream distinct() method in Java


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

Updated on: 30-Jul-2019

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements