简体   繁体   English

kotlin-如何覆盖哈希码

[英]kotlin - how to override hashcode

I have overrided the equals method of a java object. 我已经覆盖了Java对象的equals方法。 (actually the objects in kotlin but its easily understood im just overriding the equals method). (实际上是kotlin中的对象,但易于理解,只是覆盖了equals方法)。 but now inorder to maintain the equals contract i should also override the hashcode method. 但是现在为了维持平等合约,我还应该重写哈希码方法。 but im not sure how to put in a implementation for hashcode that suits the equals method. 但我不确定如何为适合equals方法的哈希码添加实现。 here is what i have so far: 这是我到目前为止所拥有的:

    data class AddressModel(
        var id_address: Int = 0,
        var id_country: Int = 0,
) {

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is AddressModel)
            return false
        else {
            if (other.id_address == this.id_address)
                return true
        }
        return false
    }
}

compiler highly suggest i override hashCode method. 编译器强烈建议我重写hashCode方法。 but i dont understand what to implement. 但我不知道该怎么执行。 in the equals override i simply want to check if addressModel has same Id as another, if it does ,then i assume its equal. 在equals重写中,我只是想检查addressModel是否具有与另一个ID相同的ID,如果确实存在,则我假定其相等。

this is what i have so far: 这是我到目前为止所拥有的:

override fun hashCode(): Int {
        return Objects.hash(id_address, id_country);
    }

but i think this is better: 但我认为这更好:

override fun hashCode(): Int {
            return Objects.hash(id_address); //base hash off same as equals the id_address
        }

this is recommended way i read but what does it mean ? 这是我阅读的推荐方式,但这是什么意思? if i added more class fields would i need to add more fields to the Objects.hash method also ? 如果我添加了更多的类字段,是否还需要向Objects.hash方法添加更多的字段? i would prefer the old skool way to do it as this call requires android api 19 and i support lower (api 16). 我更喜欢用老套的方式来做,因为此调用需要android api 19,并且我支持更低的(api 16)。

to be clear, i understand that If i don't override hashcode along with equals then every instance, eg "new AddressModel(707, 867)", will have a different hashcode. 明确地说,我理解如果我不覆盖等于的哈希码,则每个实例(例如“ new AddressModel(707,867)”)将具有不同的哈希码。 and then the HashMap for example will think these objects are distinct and not equal to replace them when calculating the hash for storage. 然后,例如HashMap会认为这些对象是不同的,并且在计算存储哈希值时并不等于替换它们。 but i just dont know what to put in the hashCode implementation. 但我只是不知道要在hashCode实现中放什么。 you can show me in either kotlin or java its ok. 您可以用kotlin或Java演示给我看。

update: would this be sufficient: 更新:这是否足够:

override fun hashCode(): Int { return id_address.hashCode() }

Since, your #equals() depends only on id_address , it should be used in hash code calculations. 由于#equals()仅取决于id_address ,因此应在哈希代码计算中使用它。 I would prefer: 我会比较喜欢:

override fun hashCode() = Objects.hash(id_address)

Sample POJO class (dataVO) overrides hashcode and equals, @Data : Lombok 样本POJO类(dataVO)覆盖哈希码并等于@Data: Lombok

Useful Links : 有用的链接:

https://www.geeksforgeeks.org/override-equalsobject-hashcode-method/ https://www.geeksforgeeks.org/override-equalsobject-hashcode-method/

Why do I need to override the equals and hashCode methods in Java? 为什么需要重写Java中的equals和hashCode方法?

https://crunchify.com/how-to-override-equals-and-hashcode-method-in-java/ https://crunchify.com/how-to-override-equals-and-hashcode-method-in-java/

 @Data

    public class dataVO implements Serializable {

    private static final long serialVersionUID = 1L;
    private int id;
    private String name;

   public String toString(){ 
          return "name:"+name;  
    }  
  @Override
    public boolean equals(Object obj) {
       if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
     if (your logic) {
        return true; 
     }
     else {
       return false;
     }
   }

    @Override
    public int hashCode() {
        return id;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM