OptionalDouble幫助我們創建一個可能包含或可能不包含Double值的對象。 equals(Object obj)方法可幫助我們將此OptionalDouble對象與傳遞的對象作為參數進行比較,如果對象相等,則返回true。
如果滿足以下條件,則認為另一個對象與此OptionalDouble相等:
- 它也是一個OptionalDouble和;
- 兩個實例都不存在值;或者
- 當前值通過==彼此為“equal to”。
用法:
public boolean equals(Object obj)
參數:此方法接受obj,它是要測試是否相等的對象。
返回值:如果另一個對象是“equal to”,則此方法返回true;否則返回false。
以下示例程序旨在說明equals(Object obj)方法:
示例1:
// Java program to demonstrate
// OptionalDouble.equals(Object obj) method
import java.util.OptionalDouble;
public class GFG {
public static void main(String[] args)
{
// Create first OptionalDouble object
OptionalDouble opDouble1
= OptionalDouble.of(452.13246);
System.out.println("OptionalDouble 1: "
+ opDouble1.toString());
// Create second OptionalDouble object
OptionalDouble opDouble2
= OptionalDouble.of(452.13246);
System.out.println("OptionalDouble 2: "
+ opDouble2.toString());
// Check if these two objects are equal
// using equals(Object obj)
System.out.println("Are both objects equal: "
+ opDouble1.equals(opDouble2));
}
}
輸出:
OptionalDouble 1: OptionalDouble[452.13246] OptionalDouble 2: OptionalDouble[452.13246] Are both objects equal: true
示例2:
// Java program to demonstrate
// OptionalDouble.equals(Object obj) method
import java.util.OptionalDouble;
public class GFG {
public static void main(String[] args)
{
// Create first OptionalDouble object
OptionalDouble opDouble1
= OptionalDouble.of(3179464.92);
System.out.println("OptionalDouble 1: "
+ opDouble1.toString());
// Create second OptionalDouble object
OptionalDouble opDouble2
= OptionalDouble.of(4521.3246);
System.out.println("OptionalDouble 2: "
+ opDouble2.toString());
// Check if these two objects are equal
// using equals(Object obj)
System.out.println("Are both objects equal: "
+ opDouble1.equals(opDouble2));
}
}
輸出:
OptionalDouble 1: OptionalDouble[3179464.92] OptionalDouble 2: OptionalDouble[4521.3246] Are both objects equal: false
參考文獻: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalDouble.html#equals?(Object obj)
相關用法
- Java OptionalDouble isPresent()用法及代碼示例
- Java OptionalDouble getAsDouble()用法及代碼示例
- Java OptionalDouble orElseThrow()用法及代碼示例
- Java OptionalDouble ifPresentOrElse()用法及代碼示例
- Java OptionalDouble of(double)用法及代碼示例
- Java OptionalDouble orElseGet()用法及代碼示例
- Java OptionalDouble toString()用法及代碼示例
- Java OptionalDouble empty()用法及代碼示例
- Java OptionalDouble hashCode()用法及代碼示例
- Java OptionalDouble stream()用法及代碼示例
- Java OptionalDouble orElse(double)用法及代碼示例
- Java OptionalDouble orElseThrow(Supplier)用法及代碼示例
- Java OptionalDouble ifPresent(DoubleConsumer)用法及代碼示例
- Java Map equals()用法及代碼示例
- Java Set equals()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 OptionalDouble equals() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。