stream()方法可幫助我們通過OptionalLong獲得Long值包含的LongStream,如果存在值,則該方法返回僅包含該值的順序LongStream,否則返回空的LongStream。
用法:
public LongStream stream()
參數:此方法不接受任何內容。
返回值:此方法返回可選值作為LongStream。
以下示例程序旨在說明stream()方法:
示例1:
// Java program to demonstrate
// OptionalLong.stream() method
import java.util.OptionalLong;
import java.util.stream.LongStream;
public class GFG {
public static void main(String[] args)
{
// create a OptionalLong
OptionalLong opLong
= OptionalLong.of(163252382159472124L);
// get value as stream
LongStream out = opLong.stream();
// print value
System.out.print("Value: ");
out.forEach(System.out::print);
}
}
輸出:
示例2:
// Java program to demonstrate
// OptionalLong.stream() method
import java.util.OptionalLong;
import java.util.stream.LongStream;
public class GFG {
public static void main(String[] args)
{
// create a empty OptionalLong
OptionalLong opLong = OptionalLong.empty();
// get value as stream
LongStream out = opLong.stream();
// print value
System.out.print("length of Long Stream: "
+ out.count());
}
}
輸出:
參考文獻: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalLong.html#empty()
相關用法
- Java OptionalLong of(long)用法及代碼示例
- Java OptionalLong equals()用法及代碼示例
- Java OptionalLong getAsLong()用法及代碼示例
- Java OptionalLong toString()用法及代碼示例
- Java OptionalLong empty()用法及代碼示例
- Java OptionalLong hashCode()用法及代碼示例
- Java OptionalLong orElseGet()用法及代碼示例
- Java OptionalLong ifPresentOrElse()用法及代碼示例
- Java OptionalLong orElseThrow()用法及代碼示例
- Java OptionalLong isPresent()用法及代碼示例
- Java OptionalLong orElseThrow(Supplier)用法及代碼示例
- Java OptionalLong orElse(long)用法及代碼示例
- Java OptionalLong ifPresent(LongConsumer)用法及代碼示例
- Java Stream min()用法及代碼示例
- Java Stream.max()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 OptionalLong stream() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。