HashMap Hashtable区别

简介: 我们先看2个类的源码HashMap Hashtable实现源码可见Hashtable 继承自 Dictiionary 而 HashMap继承自AbstractMapHashtable的put方法如下public synchronized V put(K key, V value) { // Make sure the value i

我们先看2个类的源码

HashMap Hashtable实现源码

这里写图片描述

这里写图片描述

可见Hashtable 继承自 Dictiionary 而 HashMap继承自AbstractMap

Hashtable的put方法如下

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;
    int hash = key.hashCode();
    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)) {
        V old = e.value;
        e.value = value;
        return old;
        }
    }

    modCount++;
    if (count >= threshold) {
        // Rehash the table if the threshold is exceeded
        rehash();

            tab = table;
            index = (hash & 0x7FFFFFFF) % tab.length;
    }

    // Creates the new entry.
    Entry<K,V> e = tab[index];
    tab[index] = new Entry<K,V>(hash, key, value, e);
    count++;
    return null;
    }

注意1 方法是同步的
注意2 方法不允许value==null
注意3 方法调用了key的hashCode方法,如果key==null,会抛出空指针异常

HashMap的put方法如下

public V put(K key, V value) {
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key.hashCode());
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            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;
    }

注意1 方法是非同步的
注意2 方法允许key==null
注意3 方法并没有对value进行任何调用,所以允许为null

补充:
Hashtable 有一个 contains方法,容易引起误会,所以在HashMap里面已经去掉了
当然,2个类都用containsKey和containsValue方法。

- HashMap Hashtable
父类 AbstractMap Dictiionary
是否同步
k,v可否null

HashMap是Hashtable的轻量级实现(非线程安全的实现),他们都完成了Map接口,
主要区别在于HashMap允许空(null)键值(key),由于非线程安全,效率上可能高于Hashtable。

HashMap允许将null作为一个entry的key或者value,而Hashtable不允许。

HashMap把Hashtable的contains方法去掉了,改成containsvalue和containsKey。因为contains方法容易让人引起误解。

Hashtable继承自Dictionary类,而HashMap是Java1.2引进的Map interface的一个实现。

最大的不同是,Hashtable的方法是Synchronize的,而HashMap不是,在多个线程访问Hashtable时,不需要自己为它的方法实现同步,而HashMap 就必须为之提供外同步(Collections.synchronizedMap)。

Hashtable和HashMap采用的hash/rehash算法都大概一样,所以性能不会有很大的差异。

目录
相关文章
|
5月前
|
安全 Java
HashMap和Hashtable的区别
HashMap和Hashtable的区别
33 2
|
3月前
|
安全 算法
HashMap和Hashtable 的区别
HashMap和Hashtable 的区别
19 0
|
4月前
|
存储 安全 Java
Java集合框架:HashMap和HashTable的区别是什么?
Java集合框架:HashMap和HashTable的区别是什么?
27 0
|
4月前
|
存储 安全 Java
HashMap和HashTable的区别
HashMap和HashTable的区别
23 0
|
9月前
|
安全
HashMap 和 HashTable 的区别
HashMap 和 HashTable 的区别
87 0
|
存储 安全 Java
HashMap和Hashtable以及ConcurrentHashMap的区别
HashMap是在JDK1.2中引入的Map的实现类。
272 0
|
存储 安全
HashTable 与HashMap区别
HashTable 与HashMap区别
81 0
|
存储 安全 算法
HashMap和Hashtable的联系与区别
HashMap和Hashtable的联系与区别
135 0
HashMap和Hashtable的联系与区别
|
安全 Java
HashMap与HashTable的区别
HashMap与HashTable的区别
89 0
|
安全 Java 程序员
HashMap 和 Hashtable 的 6 个区别,最后一个没几个人知道!
Java核心技术 95 篇文章16 订阅 订阅专栏 HashMap 和 Hashtable 是 Java 开发程序员必须要掌握的,也是在各种 Java 面试场合中必须会问到的。 但你对这两者的区别了解有多少呢?