- HashMap vs HashTable
- HashMap 的键和值都允许 null 值存在(当 key == null 时,哈希值为 0),而 HashTable 则不行
- HashMap 不加锁,所以效率高,非线程安全;HashTable 加锁,是线程安全的,但效率低
- ConcurrentHashMap vs HashTable
- ConcurrentHashMap 是加锁版的 HashMap,是线程安全的(有一个区别,ConcurrentHashMap 的键和值都不允许 null)
- HashTable 使用 synchronized 锁住整个方法,以阻塞整个队列,一次只允许单个线程执行,效率较低
- ConcurrentHashMap 将整个存储区分割为多个段,不同段使用不同的锁(锁分离技术),因此允许多个线程同时操作,性能要优于 HashTable
哈希存储结构如下
注:node 类型为 HashMap.Node<K,V>,其实现了接口 Map.Entry<K,V>,因此可将 Node 理解为 Entry,两者意义相同
- HashMap 的存储结构结合了 ”数组“ 和 ”链表“
- “数组” 读效率高,插入效率低
- “链表” 插入效率高,读的效率低
- 哈希结构综合了两者,具有较高的读写效率
HashMap 部分源码
put 方法
public V put(K key, V value) {
//参数依次为 哈希值、key、value、onlyIfAbsent、evict
return putVal(hash(key), key, value, false, true);
}
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;
//如果数组目标位置为 null,即不存在链表时,直接插入新节点
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
//否则,先遍历链表,如果已存在 key,替换旧值;否则,插入一个新节点
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 {
//这里对链表进行遍历,查找是否存在 key,如果找不到则插入一个新节点
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;
}
}
//如果已存在 key,替换旧值
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;
}
get 方法
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
//判断是否存在链表,如果存在则遍历链表,查找 key 对应的值
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
//无限循环,遍历链表
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
Hashtable 部分源码
put 方法
//使用 synchronized 关键字,加对象内部锁
public synchronized V put(K key, V value) {
//不允许处理 null 值
if (value == null) {
throw new NullPointerException();
}
//查找已存在的 key,替换旧值
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index];
for(; entry != null ; entry = entry.next) {
if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value;
return old;
}
}
//如果不存在 key,添加一个新的 Entry
addEntry(hash, key, value, index);
return null;
}
get 方法
//使用 synchronized 关键字,加对象内部锁
public synchronized V get(Object key) {
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
return (V)e.value;
}
}
return null;
}