难道Java的Arrays.hash code散列code()实现均匀分布均匀、Arrays、Java、code

2023-09-11 23:24:17 作者:染玖

我检讨 Arrays.hash code源$ C ​​$ C(的char [] C) 我不是很确定,它适用于良好的算法在所有情况下正常工作。

 公共静态INT散列code(INT A []){
    若(a == NULL)
        返回0;

    INT结果= 1;
    对于(INT元素:一)
        结果= 31 *结果+元素;

    返回结果;
}
 

是否哈希函数实现在此真的很均匀地分布在所有的输入arrays.And为什么我们这里使用的黄金31。

解决方案

为什么要使用质数31?

这可以被分成两部分?

     为什么一个素数?   

在这里,我们要明白,我们的目标是获得一个唯一哈希code的对象,这将有助于我们找到为O(1)时间对象。

这里的关键词是唯一

  

素数

     

素数是唯一的编号。它们是独特的,a的产物   黄金与其它一些具有是唯一的最好的机会(不   一样独特的,当然素本身)由于这样的事实,一个素   来构成它。此属性用于在哈希函数。

  

为什么31号?

在有效的Java 的

因为它是一个奇素数,而它的传统,使用的素数。

它也比二的幂,允许按位少了一个 优化

Java面试题及答案 动力节点

下面是完整的报价,

  

这是第9项:总是覆盖   当你重写hash code等于:

     

值31的选择,因为它是一个奇素数。如果它是偶数和   乘法溢出时,信息会丢失,如   乘以2就等于移位。使用的优点   黄金目前还不太清楚,但它是传统的。

     

31一个很好的特性是乘法可以通过更换   移(§15.19)和减法获得更好的性能:

     

31 * I ==(ⅰ&其中;小于5) - I现代的VM做这种优化的   自动。

     

而在这个项目的产量相当好的哈希函数配方,   它不会产生国家的最先进的哈希函数,也不是Java的   平台库提供这种散列函数的版本1.6。   写这样的散列函数是一个研究课题,最好还是留给   数学家和理论计算机科学家。

     

或许该平台的后续版本将提供先进设备,最先进的   哈希函数的类和实用方法,使平均   程序员构造这样的散列函数。在此期间,该   在这个项目中描述的技术应该是适合大多数   应用程序。

这是一个非常的好来源。

I review the source code of Arrays.hashCode(char[] c) I am not very confirm that the algorithm it applies well work well in all cases.

    public static int hashCode(int a[]) {
    if (a == null)
        return 0;

    int result = 1;
    for (int element : a)
        result = 31 * result + element;

    return result;
}

Does the hash function implement here really uniformly distributes the all the input arrays.And Why we use prime 31 here .

解决方案

Why use the prime number 31?

This can be split in two parts?

Why a prime number?

Here we need to understand that our goal is to get a unique HashCode for an object which will help us to find that object in O(1) time.

The key word here, is unique.

Primes

Primes are unique numbers. They are unique in that, the product of a prime with any other number has the best chance of being unique (not as unique as the prime itself of-course) due to the fact that a prime is used to compose it. This property is used in hashing functions.

.

Why number 31?

From Effective Java

Because it's an odd prime, and it's "traditional" to use primes.

It's also one less than a power of two, which permits for bitwise optimization

Here's the full quote,

from Item 9: Always override hashCode when you override equals:

The value 31 was chosen because it's an odd prime. If it were even and multiplication overflowed, information would be lost, as multiplication by 2 is equivalent to shifting. The advantage of using a prime is less clear, but it is traditional.

A nice property of 31 is that the multiplication can be replaced by a shift (§15.19) and subtraction for better performance:

31 * i == (i << 5) - i Modern VMs do this sort of optimization automatically.

While the recipe in this item yields reasonably good hash functions, it does not yield state-of-the-art hash functions, nor do Java platform libraries provide such hash functions as of release 1.6. Writing such hash functions is a research topic, best left to mathematicians and theoretical computer scientists.

Perhaps a later release of the platform will provide state-of-the-art hash functions for its classes and utility methods to allow average programmers to construct such hash functions. In the meantime, the techniques described in this item should be adequate for most applications.

This is a very Good source.