Tutorialspoint

1 Answer
Nancy Den

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

The syntax is as follows:

OptionalDoublemin()

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 elements to the stream:

DoubleStream doubleStream = DoubleStream.of(67.9, 89.9, 10.5, 95.8, 49.6);

Get the maximum element from the DoubleStream:

OptionalDouble res = doubleStream.max();

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

Example

 Live Demo

import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream = DoubleStream.of(67.9, 89.9, 10.5, 95.8, 49.6);
      OptionalDouble res = doubleStream.max();
      System.out.println("Minimum element: ");
      if (res.isPresent()) {
         System.out.println(res.getAsDouble());
      } else {
         System.out.println("Nothing!");
      }
   }
}

Output

Minimum element:
95.8

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.