stream()方法可幫助我們獲取OptionalDouble包含的Double值作為DoubleStream。如果存在一個值,則該方法返回僅包含該值的順序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()
相關用法
- Java OptionalDouble isPresent()用法及代碼示例
- Java OptionalDouble getAsDouble()用法及代碼示例
- Java OptionalDouble hashCode()用法及代碼示例
- Java OptionalDouble equals()用法及代碼示例
- Java OptionalDouble empty()用法及代碼示例
- Java OptionalDouble orElseThrow()用法及代碼示例
- Java OptionalDouble orElseGet()用法及代碼示例
- Java OptionalDouble ifPresentOrElse()用法及代碼示例
- Java OptionalDouble toString()用法及代碼示例
- Java OptionalDouble of(double)用法及代碼示例
- Java OptionalDouble orElse(double)用法及代碼示例
- Java OptionalDouble orElseThrow(Supplier)用法及代碼示例
- Java OptionalDouble ifPresent(DoubleConsumer)用法及代碼示例
- Java Stream min()用法及代碼示例
- Java Stream.max()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 OptionalDouble stream() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。