簡體   English   中英

覆蓋哈希碼和等於方法在Java中?

[英]Overriding hashcode and equals method in java?

我有以下課程:

public class Sample implements java.io.Serializable{

  //POJO with two fields and getters/setters

   private String name;
   private Integer id;

   //This POJO does not override equals() and hashCode()
}

public class B{
 private Sample sample;

  //here i need override hashcode and equals() based on **sample** property.


}

當我嘗試覆蓋B類中的equals()和hashCode()時,在Eclipse中收到以下錯誤。

字段類型com.mypackage.Sample不實現hashCode()和equals()-結果代碼可能無法正常工作。

現在如何基於Sample屬性比較兩個B實例是否相等? 我無法修改Sample類。

您在尋找以下內容嗎? 嘗試一下,因為從您的問題中我想您也希望比較Sample類的內容。

class Sample implements java.io.Serializable{

    //POJO with two fields and getters/setters

    private String name;
    private Integer id;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

    //This POJO does not override equals() and hashCode()
}

public class Beta implements Comparable{
    private Sample sample;

    public Sample getSample() {
        return sample;
    }

    public void setSample(Sample sample) {
        this.sample = sample;
    }

    @Override
    public int compareTo(Object o) {

        if(!(o instanceof Beta)){
            return -1;
        }
        }if(((Beta)o).getSample().getName().equals(this.sample.getName())){
                return 0; // return true if names are equal
            }
            if(((Beta)o).getSample().getId().equals(this.sample.getId())){
            //if name are notequal and IDs are equal, do what you need to do
            }
        return -1;
    }

    public static void main(String[] args) {
        Beta b = new Beta();
        Sample s = new Sample();
        s.setId(10);
        s.setName("Name1");
        b.setSample(s);

        Beta b2 = new Beta();
        Sample s2 = new Sample();
        s2.setId(20);
        s2.setName("Name2");
        b2.setSample(s2);

        System.out.println(b2.compareTo(b));

        Beta b3 = new Beta();
        Sample s3 = new Sample();
        s3.setId(10);
        s3.setName("Name1");
        b3.setSample(s3);

        System.out.println(b3.compareTo(b));
    }
}

覆蓋方法

class Sample implements java.io.Serializable{

    //POJO with two fields and getters/setters

    private String name;
    private Integer id;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

    //This POJO does not override equals() and hashCode()
}

public class Beta /*implements Comparable*/{
    private Sample sample;

    public Sample getSample() {
        return sample;
    }

    public void setSample(Sample sample) {
        this.sample = sample;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Beta other = (Beta) obj;

        if ((this.getSample() == null) && (other.getSample() == null)){
            return true;
        }
        if ((this.getSample().getId().equals(other.getSample().getId())) && (this.getSample().getName().equals(other.getSample().getName()))) {
            return true;
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + (this.getSample().getName() != null ? this.getSample().getName().hashCode() : 0);
        hash = 53 * hash + (this.getSample().getId() != null ? this.getSample().getId().hashCode() : 0);
        return hash;
    }


/*  @Override
    public int compareTo(Object o) {

        if(!(o instanceof Beta)){
            return -1;
        }
        if(((Beta)o).getSample().getId().equals(this.sample.getId()) && ((Beta)o).getSample().getName().equals(this.sample.getName())){
            return 0;
        }
        return -1;
    }*/

    public static void main(String[] args) {
        Beta b = new Beta();
        Sample s = new Sample();
        s.setId(10);
        s.setName("Name1");
        b.setSample(s);

        Beta b2 = new Beta();
        Sample s2 = new Sample();
        s2.setId(20);
        s2.setName("Name2");
        b2.setSample(s2);

        System.out.println(b2.equals(b));

        Beta b3 = new Beta();
        Sample s3 = new Sample();
        s3.setId(10);
        s3.setName("Name1");
        b3.setSample(s3);

        System.out.println(b3.equals(b));
    }

如果未顯式重寫.equals() ,則將僅基於其引用對它們進行比較(盡管沒有equals() ,每個對象都從Object繼承一個)。 如果只希望根據Sample來比較B ,則只需執行以下操作:

@Override
public boolean equals(Object o)
{
     if (o istanceof B)
     {
         return sample.equals(o.sample)
     }

     return false;
}

另外,您還應該重寫hashCode() (和compareTo() )以保持equals()hashCode()之間的約定 因此,您還應該具有以下內容:

@Override
public int hashCode()
{
    return sample.hashCode();
}

編輯(以回應評論):

我的要求是,首先我需要對照Sample的“ name”屬性檢查equals屬性。 如果名稱相等,則兩個對象相等。 如果名稱不相等,那么我需要根據示例的“ ID”屬性檢查是否相等。 我怎樣才能做到這一點? 謝謝!

確定是否Samples是等價應在處理Sample通過重寫, equals() 如果Sample equals()基於nameid ,則可以。 如果您想比較B Samples與正常情況下進行比較的方式不同,那么如果您使用Sample hashCode()equals() ,則您將無法維持B equals()hashCode()之間的約定。 ,這意味着您的B hashCode()equals()應該不能從Sample調用equals()hashCode() 有關如何根據特定字段進行覆蓋的信息,請參見本教程

暫無
暫無

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

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