Java OptionalDouble stream()方法及示例
stream() 方法帮助我们以DoubleStream的形式获得OptionalDouble所包含的双倍值。如果一个值存在,该方法返回一个只包含该值的连续DoubleStream,否则返回一个空的DoubleStream。
语法
public DoubleStream stream()
参数: 此方法不接受任何东西。
返回值: 该方法以DoubleStream的形式返回可选的值。
下面的程序说明了stream()方法:
程序1 :
// Java program to demonstrate
// OptionalDouble.stream() method
import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
public class GFG {
public static void main(String[] args)
{
// create a OptionalDouble
OptionalDouble opDouble
= OptionalDouble.of(.3203);
// get value as stream
DoubleStream out = opDouble.stream();
// print value
System.out.print("Value: ");
out.forEach(System.out::print);
}
}
输出:
程序2 :
// Java program to demonstrate
// OptionalDouble.stream() method
import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
public class GFG {
public static void main(String[] args)
{
// create a empty OptionalDouble
OptionalDouble opDouble
= OptionalDouble.empty();
// get value as stream
DoubleStream out = opDouble.stream();
// print value
System.out.print("length of Double Stream: "
+ out.count());
}
}
输出:
参考文献: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalDouble.html#empty()