DoubleStream distinct() method in Java

Java 8ProgrammingObject Oriented Programming

The distinct() method of the DoubleStream class returns a stream consisting of the distinct elements of this stream.

The syntax is as follows

DoubleStream distinct()

To use the DoubleStream class in Java, import the following package

import java.util.stream.DoubleStream;

Create DoubleStream and add some elements to the stream

DoubleStream doubleStream = DoubleStream.of(39.8, 78.7, 64.7, 78.7, 47.8, 89.7, 78.7);

Now, to get the distinct elements, use the distinct() method

doubleStream.distinct()

The following is an example to implement DoubleStream distinct() method in Java. We have repeated elements in the stream

Example

 Live Demo

import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream = DoubleStream.of(39.8, 78.7, 64.7, 78.7, 47.8, 89.7, 78.7);
      System.out.println("Distinct elements...");
      doubleStream.distinct().forEach(System.out::println);
   }
}

Output

Distinct elements...
39.8
78.7
64.7
47.8
89.7
raja
Published on 26-Mar-2019 12:11:14
Advertisements