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