Hashtable principle of analytic

    // 初始化,默认初始化大小为11,区别于HashMap的默认初始化大小16
	public Hashtable() {
        this(11, 0.75f);
    }
	
    public synchronized V put(K key, V value) {
        // Make sure the value is not null
        if (value == null) {
            throw new NullPointerException();
        }

        // Makes sure the key is not already in the hashtable.
        Entry<?,?> tab[] = table;
		// 直接使用对象的hashCode进行计算对象的数组索引
        int hash = key.hashCode();
		// 保留对象hash值的低7*8+3=59位,对tab数组的长度进行取模,来获取对应的位置索引
        int index = (hash & 0x7FFFFFFF) % tab.length;
        @SuppressWarnings("unchecked")
        Entry<K,V> entry = (Entry<K,V>)tab[index];
		// 如果已经存在,就添加到链表的next
        for(; entry != null ; entry = entry.next) {
            if ((entry.hash == hash) && entry.key.equals(key)) {
                V old = entry.value;
                entry.value = value;
                return old;
            }
        }
		// 否则直接进行添加
        addEntry(hash, key, value, index);
        return null;
    }	
	
    public synchronized V get(Object key) {
        Entry<?,?> tab[] = table;
        int hash = key.hashCode();
		// 首先计算位置索引,方法和put时的算法保持一致
        int index = (hash & 0x7FFFFFFF) % tab.length;
		// 从该位置获取到Entry对象,并遍历整个链表,判断是否存在和参数key相同的key,有则返回其value
        for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
                return (V)e.value;
            }
        }
		// 否则返回null
        return null;
    }
	
	// 返回Hashtable的迭代器,其实和HashMap的iterator相同。HashMap中的iterator只是在Collection集合框架中对迭代器进行了统一
    public synchronized Enumeration<V> elements() {
        return this.<V>getEnumeration(VALUES);
    }

	// Hashtable 的迭代器遍历示例
	Hashtable<String,String> ht = new Hashtable<String,String>();
	ht.put("1", "1") ;
	ht.put("2", "2") ;
	
	Enumeration<String> elements = ht.elements() ; 
	while (elements.hasMoreElements()){
		String str = elements.nextElement() ;
		System.out.println(str);
	}
	

characteristic:

1.Hashtable thread safe, add all manner of methods in the corresponding synchronization synchronized keyword

2. Correlation of 1, the use of synchronized reduces the processing speed, efficiency is lower than HashMap

3. The initial size of 11, extended to 2 * old + 1, HashMap initial size is 16, and the extension must be a power of 2

4. calculating the position index, using a hash value of the object is calculated directly, but the final modulo operation need to use the divider, the divider consuming. HashMap formation of array length to ensure power of 2, need to do division position index value thus calculated, help to improve the efficiency. But will increase the chance of data collision, in order to reduce the probability of collision, HashMap after obtaining the hash, hash will do some simple calculations to help break up the data, reduce collisions, and at the same time will not offset the division is not brought about to raise efficiency.

5.Hashtable inherited Dictionary Dictionary class, which is obsolete, Hashtable also obsolete. HashMap inherited from AbstractMap. But both implements Map interface.

 

 

 

 

Published 87 original articles · won praise 9 · views 80000 +

Guess you like

Origin blog.csdn.net/yunzhonghefei/article/details/104064103