HashMap 的 hash函数 jdk1.8

来源:互联网 发布:nba比赛个人数据统计 编辑:程序博客网 时间:2024/06/05 04:57
/**
     * Computes key.hashCode() and spreads (XORs) higher bits of hash
     * to lower.  Because the table uses power-of-two masking, sets of
     * hashes that vary only in bits above the current mask will
     * always collide. (Among known examples are sets of Float keys
     * holding consecutive whole numbers in small tables.)  So we
     * apply a transform that spreads the impact of higher bits
     * downward. There is a tradeoff between speed, utility, and
     * quality of bit-spreading. Because many common sets of hashes
     * are already reasonably distributed (so don't benefit from
     * spreading), and because we use trees to handle large sets of
     * collisions in bins, we just XOR some shifted bits in the
     * cheapest possible way to reduce systematic lossage, as well as
     * to incorporate impact of the highest bits that would otherwise
     * never be used in index calculations because of table bounds.
     */
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);

    }

言下之意,做一个简单的bit移位,使得高位能够作用于hash。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 狗身上有腥臭味怎么办 狗狗不想养了怎么办 药流2天后没出血怎么办 药流当天血多怎么办 怀孕四十天不想要怎么办 人流后出血量大怎么办 药流一直不出血怎么办 药流一直不见红怎么办 药流12天还流血怎么办? 把别人车刮了怎么办 车划掉漆了怎么办啊 倒车时碰了车怎么办 车头掉了一点漆怎么办 新买的车被刮了怎么办 白色车车头漆掉了一小块怎么办 临时牌照丢了1张怎么办 临牌遗失了一张怎么办 临牌只有一张了怎么办 临时牌丢了一个怎么办 药流出血特别多怎么办 新车刮擦了一点怎么办 自己的车撞墙了怎么办 新车被撞了个坑怎么办 车门被刮变形了怎么办 撞到别人的新车怎么办 新车掉了一点漆怎么办 新车擦了点漆怎么办 车挂了一点漆怎么办 第一天上班中途想走了怎么办 车子前脸裂开了怎么办 不想干了想辞职怎么办 药流期间老是吐怎么办 在工厂上班辞工后没发工资怎么办 培训期三天想走怎么办 药流吃了药吐了怎么办 工作3天不下去怎么办 在新公司融不进去怎么办 药流吃药吐了怎么办 药流期间发烧了怎么办 药流时第一天出现呕吐怎么办有事吗 药流第一天忘记第二次吃药了怎么办