GetHash code相似的价值观 [英] GetHashCode for similar values

查看:127
本文介绍了GetHash code相似的价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

 公共类Foo
{
    INT年;
    字符串名称;
    INT类;
}
 

下面是一些示例数据:

  2012 Test1的1000
2012 Test2的1000年
2012 Test3的1000
2012 TEST4 1000
2012 TEST4 10
...
 

如果我重写GetHash code所有的结果都非常相似:

 返回年^ ^名称类别;

INT哈希= 13;
    哈希散列= * 33 + year.GetHash code();
    哈希散列= * 33 + name.GetHash code();
    哈希散列= * 33 + category.GetHash code();
    返回哈希;
 

一个好的哈希函数(与最大分布)这种情况是什么?

编辑: 也许我的散列桶的理解是错误的。去类似的散列值相同的桶?

 测试1.GetHash code() - > -1556460260
Test2的.GetHash code() - > -1556460257
 

解决方案

有一件事我会建议是检查字符串对象为null 或没有。

的实施似乎要被罚款,这将是类似的,但散列codeS应该是不同的主要目的是让他们在不同的水桶土地,从而帮助了进一步的操作。

 公众诠释哈希code(){//假设一年和类别都是字符串,如姓名。
    INT哈希= 31;
    哈希散列= * 331 +(!?this.year = NULL t​​his.year.Gethash code():0);
    哈希散列= * 331 +(!?this.name = NULL t​​his.name.Gethash code():0);
    哈希散列= * 331 +(!?this.category = NULL t​​his.category.Gethash code():0);

    返回哈希;
}
 

我学到同时覆盖散列code中的几个步骤;

  
      
  1. 选择一个黄金散列如5,7,17或31(素数为乱码,导致不同的散列code代表不同的对象)。
  2.   
  3. 在拍摄另一主要作为乘数比哈希不同的是不错的。
  4.   
  5. 在计算哈希值code的每个成员,并将它们添加到最终的哈希值。重复此为其参与平等的所有成员。
  6.   
  7. 返回哈希值。
  8.   

I have the following class:

public class Foo
{
    int year;       
    string name;    
    int category;   
}

Here is some example data:

2012    Test1   1000
2012    Test2   1000
2012    Test3   1000    
2012    Test4   1000
2012    Test4   10
...

If I override GetHashCode all results are very similiar:

return year ^ name ^ category;

int hash = 13;
    hash = hash * 33 + year.GetHashCode();
    hash = hash * 33 + name.GetHashCode();
    hash = hash * 33 + category.GetHashCode();
    return hash; 

What is a good hash function (with maximal distribution) for this situation?

Edit: Perhaps my understanding of the hash buckets is wrong. Go similar hash values to the same bucket?

"Test1".GetHashCode() --> -1556460260
"Test2".GetHashCode() --> -1556460257

解决方案

One of the things I would recommend is to check if the String object is null or not.

The implementation seems to be fine, it would be similar but the hashcodes should be different as the main objective is to make them land in different buckets, hence helping out for further operations.

   public int hashCode() {    // Assuming year and category are String like name.
    int hash = 31;
    hash = hash * 331 + (this.year != null ? this.year.GethashCode() : 0);
    hash = hash * 331 + (this.name != null ? this.name.GethashCode() : 0);
    hash = hash * 331 + (this.category != null ? this.category.GethashCode() : 0);

    return hash;
}

The few steps that I have learnt while Overriding hashCode are;

  1. Choose a prime hash e.g. 5, 7, 17 or 31 (prime number as hash, results in distinct hashcode for distinct object).
  2. Take another prime as multiplier different than hash is good.
  3. Compute hashcode for each member and add them into final hash. Repeat this for all members which participated in equals.
  4. Return hash.

这篇关于GetHash code相似的价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆