简体   繁体   English

hashCode()是否必须返回素数?

[英]Does hashCode() have to return a prime number?

The signature of the hashCode() method is hashCode()方法的签名是

public int hashCode(){
    return x;
}

in this case x must be an int(primitive) but plz can anyone explain it to me that the number which the hashCode() returns must be a prime number, even number...etc or there is no specification ? 在这种情况下,x必须是int(primitive),但是plz谁能向我解释hashCode()返回的数字必须是质数,甚至是number ... etc或没有任何规范? the reason behind i am asking this question is i have seen it in different ids the auto generated code always returns a prime number, so i need to know why? 我问这个问题的原因是我在不同的ID中看到了它,自动生成的代码始终返回素数,所以我需要知道为什么吗?

thanks in advance 提前致谢

Actually there is a pretty well-specified contract concerning hashCode() : 实际上,有关hashCode()约定非常明确:

Returns a hash code value for the object. 返回对象的哈希码值。 This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable . 支持此方法是为了使哈希表受益,例如java.util.Hashtable提供的哈希表。 The general contract of hashCode is: hashCode的一般约定为:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. 在Java应用程序的执行过程中,只要在同一对象上多次调用它,则hashCode方法必须一致地返回相同的整数,前提是未修改该对象的equals比较中使用的信息。 This integer need not remain consistent from one execution of an application to another execution of the same application. 从一个应用程序的执行到同一应用程序的另一执行,此整数不必保持一致。
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. 如果根据equals(Object)方法,两个对象相等,则在两个对象中的每个对象上调用hashCode方法必须产生相同的整数结果。
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. 根据equals(java.lang.Object)方法,如果两个对象不相等,则不需要在两个对象中的每个对象上调用hashCode方法必须产生不同的整数结果。 However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. 但是,程序员应该意识到,为不相等的对象生成不同的整数结果可能会提高哈希表的性能。

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. 在合理可行的范围内,由Object类定义的hashCode方法确实为不同的对象返回不同的整数。 (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.) (通常通过将对象的内部地址转换为整数来实现,但是Java™编程语言不需要此实现技术。)

Documentation for the java.lang.Object class java.lang.Object类的文档

What you return there is up to you, if you want to, you can just emit 0 for every object instance. 返回的内容取决于您,如果您愿意 ,可以为每个对象实例发出0 It might be a bad idea but perfectly valid. 这可能是一个坏主意,但完全有效。 It's by no means always a prime number. 它绝不是素数。 As stated in the documentation the default implementation just returns the object's internal address—this is consistent with the default implementation of equals checking for reference equality. 如文档中所述,默认实现仅返回对象的内部地址,这与equals检查引用相等性的默认实现一致。

Often you can construct the hash from invoking the hashCode method on the fields that make up your object's state, such as for example: 通常,您可以通过在构成对象状态的字段上调用hashCode方法来构造哈希,例如:

class Person {
    private String firstName;
    private String lastName;

    @Override
    public int hashCode() {
        return firstName.hashCode() + lastName.hashCode();
    }
}

Keep in mind to update both hashCode and equals , though, whenever what you consider the representation of the object's state changes. 但是,无论何时考虑对象状态表示的更改,都请记住同时更新hashCodeequals

It's about efficiency . 这与效率有关。

Prime number is NOT a must to hashCode, even hashCode can return a constant int. 素数对于hashCode来说不是必须的,即使hashCode可以返回常量int。 But for efficiency, prime number leads good performance, while constant hashCode leads the worst performance (hash table deteriorates be a list). 但是为了提高效率,素数会带来良好的性能,而常量hashCode会导致最差的性能(哈希表会成为列表)。

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

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