HashTable vs HashMap


转载请注明出处:http://blog.csdn.net/crazy1235/article/details/76686891


关于HashMap的分析,请详见下面这两篇文章:


以**JDK1.7**版本的HashTable和HashMap为例来分析!

HashTable vs HashMap

HashMap和HashTable有什么区别?

  • HashMap是非线程安全的,HashTable是线程安全的。

  • HashMap的键和值都允许有null值存在,而HashTable则不行。

  • 因为线程安全的问题,HashMap效率比HashTable的要高。

  • HashTable的key和value都不允许为null值,而HashMap的key和value则都是允许null值的。

  • HashTable的迭代器使用Enumeration,HashMap的迭代器使用Iterator。

  • Java 5提供了ConcurrentHashMap,它是HashTable的替代,比HashTable的扩展性更好。

  • -

这里写图片描述

(图片转自网路)

类比着HashMap来说:

public class HashMap<K,V>
    extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable
public class Hashtable<K,V>
    extends Dictionary<K,V>
    implements Map<K,V>, Cloneable, java.io.Serializable

HashTable 集成了 Dictionary 这个类。


构造函数

public Hashtable() {
        this(11, 0.75f); // 默认容量为11,加载因子为0.75
    }
public Hashtable(int initialCapacity) {
        this(initialCapacity, 0.75f);
    }
public Hashtable(Map<? extends K, ? extends V> t) {
        this(Math.max(2*t.size(), 11), 0.75f);
        putAll(t);
    }
public Hashtable(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal Load: "+loadFactor);

        if (initialCapacity==0)
            initialCapacity = 1;
        this.loadFactor = loadFactor;
        table = new Entry[initialCapacity];
        threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
        useAltHashing = sun.misc.VM.isBooted() &&
                (initialCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
    }

hash函数

transient final int hashSeed = sun.misc.Hashing.randomHashSeed(this);
private int hash(Object k) {
        if (useAltHashing) {
            if (k.getClass() == String.class) {
                return sun.misc.Hashing.stringHash32((String) k);
            } else {
                int h = hashSeed ^ k.hashCode();
                // 这部分的hash运算与HashMap中的一样

                // 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);
             }
        } else  {
            return k.hashCode();
        }
    }

useAltHashing 为 boolean,其如果为真,则执行另一散列的字符串键,以减少由于弱哈希计算导致的哈希冲突的发生。

// 最大数组长度
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
// 当容量超过阈值的时候,会调用rehash()函数来重新构造table数组
protected void rehash() {
        int oldCapacity = table.length;
        Entry<K,V>[] oldMap = table;

        // 旧数组长度扩大一倍+1
        int newCapacity = (oldCapacity << 1) + 1;
        if (newCapacity - MAX_ARRAY_SIZE > 0) {
            if (oldCapacity == MAX_ARRAY_SIZE)
                // Keep running with MAX_ARRAY_SIZE buckets
                return;
            newCapacity = MAX_ARRAY_SIZE;
        }

        // 创建新的数组
        Entry<K,V>[] newMap = new Entry[newCapacity];

        modCount++;
        // 计算新的阈值
        threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
        boolean currentAltHashing = useAltHashing;
        useAltHashing = sun.misc.VM.isBooted() &&
                (newCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
        boolean rehash = currentAltHashing ^ useAltHashing;

        // 赋值给全局变量
        table = newMap;

        // 遍历旧数组,重新计算每个节点的hash值,然后添加到新数组(拉链表)中去
        for (int i = oldCapacity ; i-- > 0 ;) {
            for (Entry<K,V> old = oldMap[i] ; old != null ; ) {
                Entry<K,V> e = old;
                old = old.next;

                if (rehash) {
                    e.hash = hash(e.key);
                }
                int index = (e.hash & 0x7FFFFFFF) % newCapacity;
                e.next = newMap[index];
                newMap[index] = e;
            }
        }
    }

synchronized

与HashMap相比,HashTable的函数都加上了synchronized来进行加锁。但是这样会导致HashTable执行效率低于HashMap。

public synchronized int size() {
        return count;
    }
public synchronized boolean isEmpty() {
        return count == 0;
    }
public synchronized boolean contains(Object value) {
        if (value == null) {
            throw new NullPointerException();
        }

        Entry tab[] = table;
        for (int i = tab.length ; i-- > 0 ;) { // 遍历数组
            for (Entry<K,V> e = tab[i] ; e != null ; e = e.next) { // 遍历链表
                if (e.value.equals(value)) {
                    return true;
                }
            }
        }
        return false;
    }
// 。。。函数很多,就不一一列举了

从上面的contains()函数可以看出,HashTable内部实现也是采用“拉链法”保存数据的!

HashTable相比HashMap多了一个contains(Object value)函数,它与HashMap中的containsValue(Object value)的实现一致!


put

// 保存一个集合数据
public synchronized void putAll(Map<? extends K, ? extends V> t) {
        for (Map.Entry<? extends K, ? extends V> e : t.entrySet())
            put(e.getKey(), e.getValue());
    }
// 同样是加了锁
public synchronized V put(K key, V value) {
        // value不能为null
        if (value == null) {
            throw new NullPointerException();
        }

        // Makes sure the key is not already in the hashtable.
        Entry tab[] = table;
        int hash = hash(key);
        int index = (hash & 0x7FFFFFFF) % tab.length;
        // 判断是否有这个元素,如果有更新value,返回旧的value值
        for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
                V old = e.value;
                e.value = value;
                return old;
            }
        }

        modCount++;
        if (count >= threshold) { // 如果元素个数超过阈值,则需要重新扩容
            // 重建数组
            rehash();

            tab = table;
            hash = hash(key);
            // 计算新节点在数组中的位置
            index = (hash & 0x7FFFFFFF) % tab.length;
        }

        // 插入新节点
        Entry<K,V> e = tab[index];
        tab[index] = new Entry<>(hash, key, value, e);
        count++;
        return null;
    }

get

// 同步方法
public synchronized V get(Object key) {
        Entry tab[] = table;
        int hash = hash(key);
        int index = (hash & 0x7FFFFFFF) % tab.length;
        //遍历hash相同的那一个链表
        for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
                return e.value;
            }
        }
        return null;
    }

contains

public boolean containsValue(Object value) {
        return contains(value);
    }
public synchronized boolean containsKey(Object key) {
        Entry tab[] = table;
        int hash = hash(key);
        int index = (hash & 0x7FFFFFFF) % tab.length;
        for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
                return true;
            }
        }
        return false;
    }

remove

// 同步函数
public synchronized V remove(Object key) {
        Entry tab[] = table;
        int hash = hash(key);
        // 计算在数组中的位置
        int index = (hash & 0x7FFFFFFF) % tab.length;
        // 遍历链表
        for (Entry<K,V> e = tab[index], prev = null ; e != null ; prev = e, e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
                modCount++;
                // 链表删除节点,【当前节点的前节点指向当前节点的后续节点】
                if (prev != null) {
                    prev.next = e.next;
                } else {
                    tab[index] = e.next;
                }
                count--; // 节点个数-1
                V oldValue = e.value;
                e.value = null; // e置空
                return oldValue;
            }
        }
        return null;
    }

clear

// 清空数据
public synchronized void clear() {
        Entry tab[] = table;
        modCount++;
        // 很暴力,直接将数组各个元素置为null
        for (int index = tab.length; --index >= 0; )
            tab[index] = null;
        // 
        count = 0;
    }

遍历

private static final int KEYS = 0;
private static final int VALUES = 1;
private static final int ENTRIES = 2;
// 遍历key
 public synchronized Enumeration<K> keys() {
        return this.<K>getEnumeration(KEYS);
    }
// 遍历value
 public synchronized Enumeration<V> elements() {
        return this.<V>getEnumeration(VALUES);
    }
private <T> Enumeration<T> getEnumeration(int type) {
        if (count == 0) {
            return Collections.emptyEnumeration();
        } else {
            return new Enumerator<>(type, false);
        }
    }
// 迭代器
private <T> Iterator<T> getIterator(int type) {
        if (count == 0) {
            return Collections.emptyIterator();
        } else {
            return new Enumerator<>(type, true);
        }
    }
private transient volatile Set<K> keySet = null;
private transient volatile Set<Map.Entry<K,V>> entrySet = null;
private transient volatile Collection<V> values = null;
public Set<K> keySet() {
        if (keySet == null)
            keySet = Collections.synchronizedSet(new KeySet(), this);
        return keySet;
    }
// key set
private class KeySet extends AbstractSet<K> {
        public Iterator<K> iterator() {
            return getIterator(KEYS); // 1
        }
        public int size() {
            return count;
        }
        public boolean contains(Object o) {
            return containsKey(o);
        }
        public boolean remove(Object o) {
            return Hashtable.this.remove(o) != null;
        }
        public void clear() {
            Hashtable.this.clear();
        }
    }
 public Set<Map.Entry<K,V>> entrySet() {
        if (entrySet==null)
            entrySet = Collections.synchronizedSet(new EntrySet(), this);
        return entrySet;
    }
// EentrySet
private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
        public Iterator<Map.Entry<K,V>> iterator() {
            return getIterator(ENTRIES); // 2
        }

        public boolean add(Map.Entry<K,V> o) {
            return super.add(o);
        }

        public boolean contains(Object o) {
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry entry = (Map.Entry)o;
            Object key = entry.getKey();
            Entry[] tab = table;
            int hash = hash(key);
            int index = (hash & 0x7FFFFFFF) % tab.length;

            for (Entry e = tab[index]; e != null; e = e.next)
                if (e.hash==hash && e.equals(entry))
                    return true;
            return false;
        }

        public boolean remove(Object o) {
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
            K key = entry.getKey();
            Entry[] tab = table;
            int hash = hash(key);
            int index = (hash & 0x7FFFFFFF) % tab.length;

            for (Entry<K,V> e = tab[index], prev = null; e != null;
                 prev = e, e = e.next) {
                if (e.hash==hash && e.equals(entry)) {
                    modCount++;
                    if (prev != null)
                        prev.next = e.next;
                    else
                        tab[index] = e.next;

                    count--;
                    e.value = null;
                    return true;
                }
            }
            return false;
        }

        public int size() {
            return count;
        }

        public void clear() {
            Hashtable.this.clear();
        }
    }
public Collection<V> values() {
        if (values==null)
            values = Collections.synchronizedCollection(new ValueCollection(),
                                                        this);
        return values;
    }
// value collection
private class ValueCollection extends AbstractCollection<V> {
        public Iterator<V> iterator() {
            return getIterator(VALUES); // 1
        }
        public int size() {
            return count;
        }
        public boolean contains(Object o) {
            return containsValue(o);
        }
        public void clear() {
            Hashtable.this.clear();
        }
    }

从上面的函数可以看出,都离不开【Enumerator】这个类。

private class Enumerator<T> implements Enumeration<T>, Iterator<T>

Enumeration

Enumeration是java.util中的一个接口类,在Enumeration中封装了有关枚举数据集合的方法,与Iterator差不多,用来遍历集合中的元素。

枚举Enumeration只提供了遍历VectorHashtable类型集合元素的功能,这种类型的集合对象通过调用elements()方法获取一个Enumeration对象 然后Enumeratino对象再调用以下方法来对集合中的元素进行遍历。


Iterator和Enumeration区别

接口函数不同
public interface Enumeration<E> {
    // 
    boolean hasMoreElements();

    // 
    E nextElement();
}
public interface Iterator<E> {
    // 
    boolean hasNext();

    // 
    E next();

    // 
    void remove();
}
  • 可以看出最大的区别是 Iterator支持删除元素
Iterator支持fail-fast机制,而Enumeration不支持
  • Enumeration 是JDK 1.0添加的接口。使用到它的函数包括Vector、Hashtable等类,这些类都是JDK 1.0中加入的,Enumeration存在的目的就是为它们提供遍历接口。Enumeration本身并没有支持同步,而在Vector、Hashtable实现Enumeration时,添加了同步。

  • 而Iterator 是JDK 1.2才添加的接口,它也是为了HashMap、ArrayList等集合提供遍历接口。Iterator是支持fail-fast机制的:当多个线程对同一个集合的内容进行操作时,就可能会产生fail-fast事件。


fail-fast 机制

fail-fast 机制是java集合(Collection)中的一种错误机制。

当某一个线程A通过iterator去遍历某集合的过程中,若该集合的内容被其他线程所改变了;

那么线程A访问集合时,就会抛出ConcurrentModificationException异常,产生fail-fast事件。

在分析HashMap源码的时候就说道了这个机制:

HashMap抛出 ConcurrentModificationException 异常


参考

http://www.cnblogs.com/skywang12345/p/3308762.html
http://blog.csdn.net/chenssy/article/details/38151189
http://www.cnblogs.com/skywang12345/p/3311275.html
https://www.zhihu.com/question/20581065
http://www.jb51.net/article/84900.htm

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值