簡體   English   中英

為什么這個String.equals()方法總是返回false?

[英]Why does this String.equals() method always return false?

我有一個類,我使用Eclipse方法生成器實現了自定義hashCode()和equals(Object)方法。 該類的每個對象都有一個名為mUid的String字段,該字段應該是唯一的,足以確定相等性。 重寫的方法如下所示:

    @Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((mUid == null) ? 0 : mUid.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    DataSource other = (DataSource) obj;
    if (mUid == null) {
        if (other.mUid != null)
            return false;
    } else if (!mUid.equals(other.mUid))
        return false;
    return true;
}

但由於某種原因,當我比較我的類的兩個對象時,它總是返回false。 我已經使用調試器逐步完成代碼,並可以確定mUid字符串是相同的。 但我的equals(Object)方法總是在行返回false

if (!mUid.equals(other.mUid)) {return false;}

這意味着String equals()方法表示字符串不相等。 但是,在調試器中,我可以看到即使它們的哈希碼也是相同的。

這是一個調試截圖: 調試截圖 有人可以解釋一下發生了什么嗎? 謝謝。

更新:它的工作原理!

我應該指出,我不直接調用equals(...)方法,而是從List<DataSource> list.contains(DataSource) 事實證明,如果我單步執行equals(Object)代碼,它會向我顯示它返回false以比較列表中的每個對象(即使存在匹配)。 但似乎List.contains()方法無論如何都返回正確的值(true或false)。 不知道為什么會這樣,但它確實有效。 if(list.contains(dataSource));之后我有一個額外的分號if(list.contains(dataSource)); 所以我無法正確地看到contains()工作正常。 感謝大家的幫助

你的方法應該有效。 我剛用這段代碼測試過它運行正常:

public class DataSource {

    private String mUid;

    public DataSource(String m) {
        mUid = m;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((mUid == null) ? 0 : mUid.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        DataSource other = (DataSource) obj;
        if (mUid == null) {
            if (other.mUid != null)
                return false;
        } else if (!mUid.equals(other.mUid))
            return false;
        return true;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DataSource ds1 = new DataSource("test");
        DataSource ds2 = new DataSource("test");
        System.out.println(ds1.equals(ds2));
    }

}

控制台輸出是“真實的”; 當我將代碼更改為DataSource ds2 = new DataSource("test1"); 它返回“假”;

This means the String equals() method is saying the strings are not equal. But, in the debugger I can see that even their hashcodes are equal.

等號哈希碼不一定代表相等的字符串。 他們的內容真的相同嗎?

嘗試這個

 if (mUid == null)
{
    if (other.mUid != null)
        return false;
} 
else if (!mUid.equalsIgnoreCase(other.mUid))
    return false;
return true;

如果你使用了Eclipse生成器,這很奇怪,並且不需要手動編輯生成的代碼。 你可以發布全班的源代碼嗎? 你擴展另一個對象嗎?

好的,我看了一下String.equals的反編譯版本:

public boolean equals(Object obj)
{
    if(this == obj)
        return true;
    if(obj instanceof String)
    {
        String s = (String)obj;
        int i = count;
        if(i == s.count)
        {
            char ac[] = value;
            char ac1[] = s.value;
            int j = offset;
            int k = s.offset;
            while(i-- != 0) 
                if(ac[j++] != ac1[k++])
                    return false;
            return true;
        }
    }
    return false;
}

所以在debbuger中,我會嘗試比較並打印2個char數組。 您可以使用toCharArray方法。 如果char確實不同,則可能是編碼問題,我會嘗試檢查Yahoo使用的編碼並確保使用它進行解碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM