DoubleStream findAny() method in Java

Java 8ProgrammingObject Oriented Programming

The findAny() method of the DoubleStream class returns an OptionalDouble describing some element of the stream, or an empty OptionalDouble if the stream is empty.

The syntax is as follows

OptionalDouble findAny()

Here, OptionalDouble is a container object which may or may not contain a double value To use the DoubleStream class in Java, import the following package

import java.util.stream.DoubleStream;

Create a DoubleStream and add some elements

DoubleStream doubleStream = DoubleStream.of(23.8, 30.2, 50.5, 78.9, 80.4, 95.8);

Now, display an element

OptionalDouble res = doubleStream.findAny();

The following is an example to implement DoubleStream findAny() method in Java

Example

 Live Demo

import java.util.*;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream = DoubleStream.of(23.8, 30.2, 50.5, 78.9, 80.4, 95.8);
      OptionalDouble res = doubleStream.findAny();
      if (res.isPresent())
         System.out.println(res.getAsDouble());
      else
         System.out.println("Nothing!");
   }
}

Output

23.8
raja
Published on 26-Mar-2019 15:02:52
Advertisements