集合框架-HashSet集合的add()方法的原始碼

ZHOU_VIP發表於2017-04-25


interface Collection {
	...
}

interface Set extends Collection {
	...
}

class HashSet implements Set {
	private static final Object PRESENT = new Object();
	private transient HashMap<E,Object> map;
	
	public HashSet() {
		map = new HashMap<>();
	}
	
	public boolean add(E e) { //e=hello,world
        return map.put(e, PRESENT)==null;
    }
}

class HashMap implements Map {
	public V put(K key, V value) { //key=e=hello,world
	
	    //看雜湊表是否為空,如果空,就開闢空間
        if (table == EMPTY_TABLE) {
            inflateTable(threshold);
        }
        
        //判斷物件是否為null
        if (key == null)
            return putForNullKey(value);
        
        int hash = hash(key); //和物件的hashCode()方法相關
        
        //在雜湊表中查詢hash值
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            //這次的e其實是第一次的world
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
                //走這裡其實是沒有新增元素
            }
        }

        modCount++;
        addEntry(hash, key, value, i); //把元素新增
        return null;
    }
    
    transient int hashSeed = 0;
    
    final int hash(Object k) { //k=key=e=hello,
        int h = hashSeed;
        if (0 != h && k instanceof String) {
            return sun.misc.Hashing.stringHash32((String) k);
        }

        h ^= k.hashCode(); //這裡呼叫的是物件的hashCode()方法

        // This function ensures that hashCodes that differ only by
        // constant multiples at each bit position have a bounded
        // number of collisions (approximately 8 at default load factor).
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }
}


hs.add("hello");
hs.add("world");
hs.add("java");
hs.add("world");



相關文章