首页 > 其他 > 详细

Hash function

时间:2017-06-11 09:27:39      阅读:213      评论:0      收藏:0      [点我收藏+]
1.the division method

h(k) = k mod m 

When using the division method, we usually avoid certain values of m. For
example, m should not be a power of 2, since if m^2p, then h(k) is just the p
lowest-order bits of k.

A prime not too close to an exact power of 2 is often a good choice for m.

 

2.The multiplication method

The multiplication method for creating hash functions operates in two steps. First,
we multiply the key k by a constant A in the range 0 < A < 1 and extract the

fractional part of kA. Then, we multiply this value by m and take the floor of the
result. In short, the hash function is
h(k)  = [m(kA mod 1)] ;
where “kA mod 1” means the fractional part of kA We tepically choose m to be a power of 2

 技术分享

 

技术分享

 

use hashCode() to get an array index

private int hash(Key x){
return (x.hashCode() & 0x7fffffff)%M;   //turn 32bit to 32bit
}

use hashCode on private type

    public int hashCode() {
        int hash = 1;
        hash = 31*hash + who.hashCode();
        hash = 31*hash + when.hashCode();
        hash = 31*hash + ((Double) amount).hashCode();
        return hash;
        // return Objects.hash(who, when, amount);
    }

 

Hash function

原文:http://www.cnblogs.com/wujunde/p/6980503.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!