OptionalDouble幫助我們創建一個可能包含或可能不包含Double值的對象。 isPresent()方法可幫助我們獲取是否在OptionalDouble對象中存在double值的答案。如果存在雙精度值,則返回true,否則返回false。
用法:
public boolean isPresent()
參數:此方法不接受任何內容。
返回值:如果存在值,則此方法返回true,否則返回false
以下示例程序旨在說明isPresent()方法:
示例1:
// Java program to demonstrate
// OptionalDouble.isPresent() method
import java.util.OptionalDouble;
public class GFG {
public static void main(String[] args)
{
// create a OptionalDouble
OptionalDouble opDouble
= OptionalDouble.of(3148.1345);
// get value using isPresent()
System.out.println("OptionalDouble"
+ " has a value= "
+ opDouble.isPresent());
}
}
輸出:
OptionalDouble has a value= true
示例2:
// Java program to demonstrate
// OptionalDouble.isPresent() method
import java.util.OptionalDouble;
public class GFG {
public static void main(String[] args)
{
// create a OptionalDouble
OptionalDouble opDouble
= OptionalDouble.empty();
// try to get that value is present or not
boolean response = opDouble.isPresent();
if (response)
System.out.println("Value present");
else
System.out.println("Value absent");
}
}
輸出:
Value absent
參考文獻: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalDouble.html#isPresent()
相關用法
- Java OptionalLong isPresent()用法及代碼示例
- Java OptionalInt isPresent()用法及代碼示例
- Java Optional isPresent()用法及代碼示例
- Java OptionalDouble ifPresentOrElse()用法及代碼示例
- Java OptionalDouble toString()用法及代碼示例
- Java OptionalDouble equals()用法及代碼示例
- Java OptionalDouble orElseThrow()用法及代碼示例
- Java OptionalDouble of(double)用法及代碼示例
- Java OptionalDouble orElseGet()用法及代碼示例
- Java OptionalDouble empty()用法及代碼示例
- Java OptionalDouble hashCode()用法及代碼示例
- Java OptionalDouble stream()用法及代碼示例
- Java OptionalDouble getAsDouble()用法及代碼示例
- Java OptionalDouble orElseThrow(Supplier)用法及代碼示例
- Java OptionalDouble ifPresent(DoubleConsumer)用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 OptionalDouble isPresent() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。