2
回答
JDK hashmap 链表数组
开发十年,就只剩下这套Java开发体系了   

发生hash冲突的时候,  hashmap底层新的元素会覆盖原有的元素, 新元素的next指向原有元素形成链表数组  ,   为什么不直接覆盖呢,如果直接覆盖的话, 就可以减少链表的存储了, 这样岂不是更节省空间吗?  保留原有的数据值是为了什么, 有什么意义吗?求各位大佬解惑

<无标签>
举报
帝孤
发帖于6天前 2回/111阅
共有2个答案 最后回答: 5天前
    /**
     * 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);
    }

 

顶部