HashSet 的 elements 为什么必定要重写 hashCode() 和 equals() 方法

先看下 HashSet 的存储原理:java

好比咱们实例化一个 HashSet 对象,而后存入一个元素:算法

Set<Integer> set = new HashSet<Integer>();
set.add(new Integer(250));

看下 HashSet 类的 构造方法 和 add() 方法源码:数据结构

private static final Object PRESENT = new Object();
private transient HashMap<E,Object> map;

public HashSet() {
    map = new HashMap<>();
}

public boolean add(E e) {
    return map.put(e, PRESENT)==null;
}

而后是 map.put() 方法的源码:app

public V put(K key, V value) {
    return putVal(hash(key), key, value, false, true);
}

而后是 putVal() 方法源码:this

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
               boolean evict) {
    Node<K,V>[] tab; Node<K,V> p; int n, i;
    if ((tab = table) == null || (n = tab.length) == 0)
        n = (tab = resize()).length;
    if ((p = tab[i = (n - 1) & hash]) == null)
        tab[i] = newNode(hash, key, value, null);
    else {
        Node<K,V> e; K k;
        if (p.hash == hash &&
            ((k = p.key) == key || (key != null && key.equals(k))))
            e = p;
        else if (p instanceof TreeNode)
            e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
        else {
            for (int binCount = 0; ; ++binCount) {
                if ((e = p.next) == null) {
                    p.next = newNode(hash, key, value, null);
                    if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                        treeifyBin(tab, hash);
                    break;
                }
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))
                    break;
                p = e;
            }
        }
        if (e != null) { // existing mapping for key
            V oldValue = e.value;
            if (!onlyIfAbsent || oldValue == null)
                e.value = value;
            afterNodeAccess(e);
            return oldValue;
        }
    }
    ++modCount;
    if (++size > threshold)
        resize();
    afterNodeInsertion(evict);
    return null;
}

关于 HashMap 的实现原理可参考此文:HashMap实现原理简析(哈希表)code

对于 putVal() 方法,若对数据结构不甚了解的同窗(好比我)可能会看得有些迷迷糊糊,可是没关系!对象

大致的,应该记住如下3点:get

一、将要传入 HashSet 的数据会根据系统的 hash 算法获得一个 hash 值;源码

二、根据 hash 值能够得出该数据在 hash 表中的位置;hash

三、判断该位置上是否有值,没有值则把数据插入进来;若是有值则再次判断传入的值与原值是否地址或 equals 相同,若是相同则不存,不然经过链表的方式存储到该位置。

若是两个对象 equals ,可是没有重写 hashcode ,就会致使集合中存储多个相等的对象!因此必须重写!

相关文章
相关标签/搜索