亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

等于并與 BigDecimal 進行比較

等于并與 BigDecimal 進行比較

12345678_0001 2023-08-04 14:50:32
我有一個類重寫hashCode() 和equals()- 方法。當我處理時BigDecimal,我必須compareTo()使用Objects.equals():public class MyProduct extends Product{private BigDecimal price;@Overridepublic int hashCode() {    return Objects.hash(price);}@Overridepublic boolean equals(Object obj) {        if (this == obj) return true; // is this right or should this be deleted        if (obj == null || getClass() != obj.getClass()) return false;        final Product other = (Product) obj;        // is this right?        if (price == null ? (other.price != null) : price.compareTo(other.price) != 0) {            return false;        }        return super.equals(obj);    }}我有以下問題:if (this == obj) return true;我應該從-method 中刪除該行嗎equals()?因為使用這一行,compareTo 不會被觸發,并且可能會計算出錯誤的 equals(),對嗎?equals()方法可以改進嗎?
查看完整描述

3 回答

?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

第一行只是一種優化,旨在如果兩個引用都指向同一對象,則提前返回結果。


可以price為空嗎?我認為是的,因為您正在實施中檢查它equals()。在這種情況下,您的代碼將無法工作,以防other.price萬一null。具體這里的代碼:


price.compareTo(other.price) != 0

會拋出一個NullPointerException.


你可以這樣修復它:


    @Override

    public boolean equals(Object obj) {


        if (this == obj) return true; // is this right or should this be deleted


        if (obj == null || getClass() != obj.getClass()) return false;


        final MyProduct other = (MyProduct) obj;


        // If you prefer, the below two can be replaced with a single condition

        // price != null ^ other.price != null

        // Credits to @Andreas

        if (price == null && other.price != null) {

            return false;

        }

        if (price != null && other.price == null) {

            return false;

        }

        if (other.price != null && price.compareTo(other.price) != 0) {

            return false;

        }


        return super.equals(obj);

    }

現在,您可能可以將其縮短,但我個人認為這種方式最具可讀性。


無論如何,除非您真的非常關心自定義您的equals()實現,否則我建議您使用 IDE 生成一個并堅持使用它。他們大多數時候都做得不錯,你不必擔心它會被破壞(盡管比較對BigDecimals他們來說可能很棘手,因為你不關心規模而只關心價值)。


查看完整回答
反對 回復 2023-08-04
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

我編寫了一個 utitly 方法,可用于比較兩個 BigDecimals 而不會拋出 NPE:


// returns true, if val1 is the same as val2

// can this be improved ?

public static boolean isEqual(BigDecimal val1, BigDecimal val2) {

        return !((val1 != null ^ val2 != null) || (val2 != null && val1.compareTo(val2) != 0));

    }

這可以在 equals 方法中使用:


@Override

    public boolean equals(Object obj) {

        if (this == obj) return true;

        if (obj == null || getClass() != obj.getClass()) return false;

        final MyProduct other = (MyProduct) obj;


        if(!isEqual(price, other.price)) return false;


        return super.equals(obj);

    }


查看完整回答
反對 回復 2023-08-04
?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

我找到了最簡單的方法:


public static boolean isEqual(BigDecimal val1, BigDecimal val2) {

    return val1 != null ^ val2 != null && val2 != null && val1.compareTo(val2) != 0;

}

然后在 equals() 中使用它:


public boolean equals(Object obj) {

        if (this == obj) return true;

        if (obj == null || getClass() != obj.getClass()) return false;

        final MyProduct other = (MyProduct) obj;


        if(!isEqual(price, other.price)) return false;


        return super.equals(obj);

    }


查看完整回答
反對 回復 2023-08-04
  • 3 回答
  • 0 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號