Overriding the java equals() method - not working?

后端 未结 8 1239
花落未央
花落未央 2020-11-22 02:33

I ran into an interesting (and very frustrating) issue with the equals() method today which caused what I thought to be a well tested class to crash and cause a

相关标签:
8条回答
  • 2020-11-22 03:21

    If you use eclipse just go to the top menu

    Source --> Generate equals() and hashCode()

    0 讨论(0)
  • 2020-11-22 03:26

    in Android Studio is alt + insert ---> equals and hashCode

    Example:

        @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
    
        Proveedor proveedor = (Proveedor) o;
    
        return getId() == proveedor.getId();
    
    }
    
    @Override
    public int hashCode() {
        return getId();
    }
    
    0 讨论(0)
提交回复
热议问题