Java 8中的java.util.stream.DoubleStream處理原始雙精度型。它以新的方式幫助解決諸如在數組中找到最大值,在數組中找到最小值,數組中所有元素的總和以及數組中所有值的平均值之類的問題。 DoubleStream min()返回描述此流的最小元素的OptionalDouble;如果此流為空,則返回一個空的Optional。
用法:
OptionalDouble() min() Where, OptionalDouble is a container object which may or may not contain a double value.
注意:
- 與數值比較運算符不同,此方法認為負零嚴格小於正零。
- 這是減少的特殊情況。
示例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");
}
}
}
輸出:
-18.6
示例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");
}
}
}
輸出:
-1
相關用法
- Java DoubleStream max()用法及代碼示例
- Java DoubleStream empty()用法及代碼示例
- Java DoubleStream filter()用法及代碼示例
- Java DoubleStream count()用法及代碼示例
- Java DoubleStream distinct()用法及代碼示例
- Java DoubleStream allMatch()用法及代碼示例
- Java DoubleStream boxed()用法及代碼示例
- Java DoubleStream anyMatch()用法及代碼示例
- Java DoubleStream builder()用法及代碼示例
- Java DoubleStream average()用法及代碼示例
- Java DoubleStream noneMatch()用法及代碼示例
- Java DoubleStream limit()用法及代碼示例
- Java DoubleStream peek()用法及代碼示例
- Java DoubleStream.Builder add(double t)用法及代碼示例
- Java DoubleStream reduce(double identity, DoubleBinaryOperator op)用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 DoubleStream min() in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。