DoubleStream min() in Java with examples
java.util.stream.DoubleStream in Java 8, deals with primitive doubles. It helps to solve the problems like finding maximum value in array, finding minimum value in array, sum of all elements in array, and average of all values in array in a new way. DoubleStream min() returns an OptionalDouble describing the minimum element of this stream, or an empty optional if this stream is empty.
Syntax :
OptionalDouble() min() Where, OptionalDouble is a container object which may or may not contain a double value.
Note :
- Unlike the numerical comparison operators, this method considers negative zero to be strictly smaller than positive zero.
- This is a special case of a reduction.
Example 1 :
// Java code for DoubleStream min() import java.util.*; import java.util.stream.DoubleStream; class GFG { // Driver code public static void main(String[] args) { // creating a stream DoubleStream stream = DoubleStream.of(- 9.5 , - 18.6 , 54.3 , 8.5 , 7.4 , 14.2 , 3.9 ); // OptionalDouble is a container object // which may or may not contain a // double value. OptionalDouble obj = stream.min(); // If a value is present, isPresent() will // return true and getAsDouble() will // return the value if (obj.isPresent()) { System.out.println(obj.getAsDouble()); } else { System.out.println( "-1" ); } } } |
Output :
-18.6
Example 2 :
// Java code for DoubleStream min() import java.util.*; import java.util.stream.DoubleStream; class GFG { // Driver code public static void main(String[] args) { // OptionalDouble is a container object // which may or may not contain a // double value. OptionalDouble obj = DoubleStream.empty().min(); // If a value is present, isPresent() will // return true and getAsDouble() will // return the value if (obj.isPresent()) { System.out.println(obj.getAsDouble()); } else { System.out.println( "-1" ); } } } |
Output :
-1
Recommended Posts:
- DoubleStream max() in Java with examples
- DoubleStream anyMatch() in Java with examples
- DoubleStream allMatch() in Java with examples
- DoubleStream peek() in Java with examples
- DoubleStream limit() in Java with Examples
- DoubleStream builder() in Java with Examples
- DoubleStream boxed() in Java with Examples
- DoubleStream noneMatch() in Java with examples
- DoubleStream average() in Java with Examples
- DoubleStream filter() in Java with examples
- DoubleStream count() in Java with examples
- DoubleStream empty() in Java with examples
- DoubleStream distinct() in Java with examples
- DoubleStream.Builder add(double t) in Java with Examples
- DoubleStream reduce(double identity, DoubleBinaryOperator op) in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.