简体   繁体   English

hashCode返回的确切值是什么意思?

[英]What does exact value returned by their hashCode mean?

I'm reading Effective Java 2nd Edition by Joshua Bloch. 我正在阅读Joshua Bloch的Effective Java 2nd Edition。

In this paragraph, he mentions that: 在这一段中,他提到:

Many classes in the Java platform libraries, such as String, Integer, and Date, include in their specifications the exact value returned by their hashCode method as a function of the instance value. Java平台库中的许多类(如String,Integer和Date)在其规范中包含hashCode方法返回的实际值作为实例值的函数。 This is generally not a good idea, as it severely limits your ability to improve the hash function in future releases. 这通常不是一个好主意,因为它严重限制了您在将来的版本中改进哈希函数的能力。 If you leave the details of a hash function unspecified and a flaw is found or a better hash function discovered, you can change the hash function in a subsequent release, confident that no clients depend on the exact values returned by the hash function. 如果您未指定散列函数的详细信息并发现缺陷或发现更好的散列函数,则可以在后续版本中更改散列函数,确信没有客户端依赖散列函数返回的确切值。

Can anyone please share some insights what he means by 'exact' values. 任何人都可以通过'确切'的价值观分享他的意思。 I had a look at the String implementation class but still unable to understand what he means... 我看了一下String实现类,但仍然无法理解他的含义......

Thanks in advance! 提前致谢!

From String.hashCode() : 来自String.hashCode()

Returns a hash code for this string. 返回此字符串的哈希码。 The hash code for a String object is computed as String对象的哈希码计算为

 s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 

By giving out the definition in the javadoc, people may write code that depends on exactly that hashing algorithm. 通过在javadoc中给出定义,人们可以编写完全依赖于散列算法的代码。 Changing the hash algorithm in a future release would then break that code. 在将来的版本中更改哈希算法会破坏该代码。

It means that the value returned by hashCode() is prescribed in the Javadoc. 这意味着hashCode()返回的值是在Javadoc中规定的。

eg 例如

  • String.hashCode() : String.hashCode()

    Returns a hash code for this string. 返回此字符串的哈希码。 The hash code for a String object is computed as String对象的哈希码计算为

     s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 
  • Integer.hashCode() : Integer.hashCode()

    [Returns] a hash code value for this object, equal to the primitive int value represented by this Integer object. [返回]此对象的哈希码值,等于此Integer对象表示的原始int值。

  • Date.hashCode() : Date.hashCode()

    Returns a hash code value for this object. 返回此对象的哈希码值。 The result is the exclusive OR of the two halves of the primitive long value returned by the getTime() method. 结果是getTime()方法返回的原始long值的两半的异或。 That is, the hash code is the value of the expression: 也就是说,哈希码是表达式的值:

     (int)(this.getTime()^(this.getTime() >>> 32)) 

If you look at the API documentation of java.lang.String.hashCode() , it describes exactly how the method is implemented: 如果查看java.lang.String.hashCode()的API文档,它会准确描述该方法的实现方式:

Returns a hash code for this string. 返回此字符串的哈希码。 The hash code for a String object is computed as String对象的哈希码计算为

 s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 

using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. 使用int算术,其中s [i]是字符串的第i个字符,n是字符串的长度,^表示取幂。 (The hash value of the empty string is zero.) (空字符串的哈希值为零。)

What Bloch says, is that it is a mistake that classes such as String describe the implementation details in the API documentation, because this means that programmers can count on the hashCode method being implemented this way. Bloch说的是,像String这样的类描述API文档中的实现细节是错误的,因为这意味着程序员可以依赖于以这种方式实现的hashCode方法。 If, in a future Java release, Oracle wants to implement a different, maybe more efficient algorithm to calculate a hash code for a string, then that would be a backward compatibility problem - the behaviour might change compared to previous Java versions. 如果在未来的Java版本中,Oracle希望实现一种不同的,可能更有效的算法来计算字符串的哈希码,那么这将是一个向后兼容性问题 - 与以前的Java版本相比,行为可能会发生变化。

By describing the implementation in detail in the API documentation, the way it is implemented has become part of the official specification of the Java API. 通过在API文档中详细描述实现,它的实现方式已成为Java API官方规范的一部分。

In general, API documentation should just describe what the purpose is of the method, and not exactly how it is implemented. 一般而言,API文档应该只描述方法的目的,而不是具体如何实现。

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

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