[英]Compare two objects with .equals() and == operator
我用一個String
字段構造了一個類。 然后我創建了兩個對象,我也必須使用==
運算符和.equals()
來比較它們。 這是我所做的:
public class MyClass {
String a;
public MyClass(String ab) {
a = ab;
}
public boolean equals(Object object2) {
if(a == object2) {
return true;
}
else return false;
}
public boolean equals2(Object object2) {
if(a.equals(object2)) {
return true;
}
else return false;
}
public static void main(String[] args) {
MyClass object1 = new MyClass("test");
MyClass object2 = new MyClass("test");
object1.equals(object2);
System.out.println(object1.equals(object2));
object1.equals2(object2);
System.out.println(object1.equals2(object2));
}
}
編譯后,結果顯示兩次錯誤。 如果兩個對象具有相同的字段 - “測試”,為什么它是錯誤的?
==
比較對象引用,它檢查兩個操作數是否指向同一個對象(不是等價對象,同一個對象)。
如果要比較字符串(以查看它們是否包含相同的字符),則需要使用equals
比較字符串。
在您的情況下,如果字符串匹配,則MyClass
兩個實例確實被認為是相等的,那么:
public boolean equals(Object object2) {
return object2 instanceof MyClass && a.equals(((MyClass)object2).a);
}
......但通常如果你定義一個類,還有比單場的等效性(更等同a
在這種情況下)。
旁注:如果您覆蓋equals
,您幾乎總是需要覆蓋hashCode
。 正如它在equals
JavaDoc 中所說:
請注意,每當重寫此方法時,通常都需要重寫
hashCode
方法,以維護hashCode
方法的一般約定,即相等的對象必須具有相等的哈希碼。
你應該覆蓋等於
public boolean equals (Object obj) {
if (this==obj) return true;
if (this == null) return false;
if (this.getClass() != obj.getClass()) return false;
// Class name is Employ & have lastname
Employe emp = (Employee) obj ;
return this.lastname.equals(emp.getlastname());
}
比較 2 個對象的最佳方法是將它們轉換為 json 字符串並比較字符串,這是處理復雜嵌套對象、字段和/或包含數組的對象時最簡單的解決方案。
樣本:
import com.google.gson.Gson;
Object a = // ...;
Object b = //...;
String objectString1 = new Gson().toJson(a);
String objectString2 = new Gson().toJson(b);
if(objectString1.equals(objectString2)){
//do this
}
覆蓋函數 equals() 是錯誤的。 對象“a”是String類的實例,而“object2”是MyClass類的實例。 它們是不同的類,所以答案是“假”。
看起來equals2
只是調用equals
,所以它會給出相同的結果。
您的equals2()
方法將始終返回與equals()
相同的值!!
你的代碼和我的評論:
public boolean equals2(Object object2) { // equals2 method
if(a.equals(object2)) { // if equals() method returns true
return true; // return true
}
else return false; // if equals() method returns false, also return false
}
語句a == object2
和a.equals(object2)
都將始終返回false
因為a
是string
而object2
是MyClass
的實例
您的實現必須喜歡:
public boolean equals2(Object object2) {
if(a.equals(object2.a)) {
return true;
}
else return false;
}
有了這個實現,你的兩種方法都可以工作。
您的類可能會實現 Comparable 接口以實現相同的功能。 您的類應該實現在接口中聲明的 compareTo() 方法。
public class MyClass implements Comparable<MyClass>{
String a;
public MyClass(String ab){
a = ab;
}
// returns an int not a boolean
public int compareTo(MyClass someMyClass){
/* The String class implements a compareTo method, returning a 0
if the two strings are identical, instead of a boolean.
Since 'a' is a string, it has the compareTo method which we call
in MyClass's compareTo method.
*/
return this.a.compareTo(someMyClass.a);
}
public static void main(String[] args){
MyClass object1 = new MyClass("test");
MyClass object2 = new MyClass("test");
if(object1.compareTo(object2) == 0){
System.out.println("true");
}
else{
System.out.println("false");
}
}
}
僅當兩個引用指向內存中的同一對象時,“==”運算符才返回 true。 另一方面,equals() 方法根據對象的內容返回 true。
例子:
String personalLoan = new String("cheap personal loans");
String homeLoan = new String("cheap personal loans");
//since two strings are different object result should be false
boolean result = personalLoan == homeLoan;
System.out.println("Comparing two strings with == operator: " + result);
//since strings contains same content , equals() should return true
result = personalLoan.equals(homeLoan);
System.out.println("Comparing two Strings with same content using equals method: " + result);
homeLoan = personalLoan;
//since both homeLoan and personalLoan reference variable are pointing to same object
//"==" should return true
result = (personalLoan == homeLoan);
System.out.println("Comparing two reference pointing to same String with == operator: " + result);
輸出:使用 == 運算符比較兩個字符串:false 使用 equals 方法比較具有相同內容的兩個字符串:true 使用 == 運算符比較指向同一字符串的兩個引用:true
您還可以從鏈接中獲取更多詳細信息: http : //javarevisited.blogspot.in/2012/12/difference-between-equals-method-and-equality-operator-java.html?m=1
如果不需要自定義默認的 toString() 函數,另一種方法是覆蓋 toString() 方法,該方法返回所有要比較的屬性。 然后比較兩個對象的 toString() 輸出。 我使用 IntelliJ IDEA IDE 生成了 toString() 方法,該方法在字符串中包含類名。
public class Greeting {
private String greeting;
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
return this.toString().equals(obj.toString());
}
@Override
public String toString() {
return "Greeting{" +
"greeting='" + greeting + '\'' +
'}';
}
}
object.equals 的返回類型已經是布爾值。 無需將其包裝在帶有分支的方法中。 所以如果你想比較 2 個對象,只需比較它們:
boolean b = objectA.equals(objectB);
b 已經是真或假。
當我們使用 == 時,比較的是對象的引用而不是實際的對象。 我們需要覆蓋 equals 方法來比較 Java 對象。
一些附加信息 C++ 有運算符重載,而 Java 不提供運算符重載。 java中的其他可能性還有實現Compare接口。它定義了一個compareTo方法。
比較器接口也用於比較兩個對象
這里的輸出將是 false , false 因為在第一個 sopln 語句中,您試圖將 Myclass 類型的字符串類型變量與另一個 MyClass 類型進行比較,並且由於兩者都是 Object 類型並且您使用了“==”操作符,因此它將允許將檢查保存實際內存的引用變量值,而不是內存中的實際內容。 在第二個 sopln 中,它也與您再次調用 a.equals(object2) 相同,其中 a 是 object1 中的變量。 請讓我知道您對此的發現。
簡而言之, == 比較兩個 POINTERS。
如果這兩個指針相等,那么它們都指向內存中的同一個對象(顯然與它本身具有相同的值)。
但是, .equals 將比較所指向的任何內容的 VALUES,如果它們都評估為相同的值,則返回 true。
因此,兩個單獨的字符串(即,在內存中的不同地址)總是 != 但如果它們包含相同的(以空結尾的)字符序列,則它們是 .equal 的。
在下面的代碼中,您正在調用覆蓋方法 .equals()。
public boolean equals2(Object object2) { if(a.equals(object2)) { // 在這里你調用了覆蓋方法,這就是為什么你得到兩次錯誤。 返回真; 否則返回假; }
/** Comparing two or more variables. Here the two items are compared and it will return a boolean true or false.
*
* @return equals
*/
public boolean equals(Object item)
{
String emp1 = this.toString();
String emp2 = item.toString();
return emp1.equals(emp2);
}
@Override
//In case you have more than one variable.
public String toString()
{
return a + b;
}
// a and b represent the variables to display.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.