本文所使用的源码来自jdk1.8
1、存储
HashMap之所以访问很快,内部机制是通过数组+链表(链表长度大于8则转为红黑树)来实现的。
HashMap中最顶层的结构是哈希桶数组:
transient Node<K,V>[] table;
其中,Node即表示数组的一个元素,也是一个单向链表,结构如下:
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
HashMap的目的是为了快速访问数据,数组的访问效率是最快的,而链表的遍历就慢多了。为了体现HashMap的访问速度,要预估好HashMap存储的k-v对的数量,尽量保证一半左右的数组是空的,并且要求key的hashCode的质量,使数据能够尽可能平均分配到各个Hash桶中,也就是让链表的长度尽可能短。
HashMap中默认的值是0.75,即存储的k-v对的数量建议小于数组长度*0.75。如果超出这个值,Hash桶数组会进行扩容。可以通过预估数据量以及设置threshold的值来降低HashMap的扩容的影响。
transient int size;//包含的k-v对
transient int modCount;
int threshold;//能容纳的k-v对极限,为length*loadFactor 超过需要进行扩容
final float loadFactor;//负载因子,通常为0.75
2、get方法
get方法首先对key做hash,通过(h = key.hashCode()) ^ (h >>> 16)
来计算出key对应的hash值,再通过table.length & hash值
来找出哈希桶数组(即上面说的table)中对应的下标(其实就是取余,但效率更高),即找到key所在的链表(或红黑树)。进而寻找key对应的Node。
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
//在table中找到key所在的链表,先判断链表头的项是否为所需
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;
}
3、put方法
public V put(K key, V value) {
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;
//table为空则创建
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//算出数组索引,如果为空则创建
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
//节点key存在直接覆盖value
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//是红黑树直接做put
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
//为链表,先遍历
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//链表长度大于8,转换为红黑树
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;
}
}
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;
}
4、resize方法
HashMap有扩容机制,put时如果包含的k-v对超过了允许的数量,HashMap会进行扩容,每次数组容量翻倍(初试为16,达到极限不再扩容),即新建一个新的容量翻倍的数组,再把原数据拷贝过来。
5、多线程相关
HashMap不是线程安全的,多线程代码中要慎用。
6、entrySet,keySet。
由于Set一般都是使用Map来实现的,因而不应该维持一个set或者新建一个set。这些方法原理都一样,我们以entrySet为例来阐述。
public Set<Map.Entry<K,V>> entrySet() {
Set<Map.Entry<K,V>> es;
return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
}
EntrySet是一个内部类,包含Set的各种行为,就以最常见的遍历来说吧。常规做法是通过iterator()来遍历,iterator其实是返回一个HashIterator的子类,next()和HashNext方法其实是借助于对HashMap的table(即哈希桶数组)的遍历来实现的。
public final Iterator<Map.Entry<K,V>> iterator() {
return new EntryIterator();
}
final class EntryIterator extends HashIterator
implements Iterator<Map.Entry<K,V>> {
public final Map.Entry<K,V> next() { return nextNode(); }
}
abstract class HashIterator {
Node<K,V> next; // next entry to return
Node<K,V> current; // current entry
int expectedModCount; // for fast-fail
int index; // current slot
final Node<K,V> nextNode() {
Node<K,V>[] t;
Node<K,V> e = next;
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
if (e == null)
throw new NoSuchElementException();
if ((next = (current = e).next) == null && (t = table) != null) {
do {} while (index < t.length && (next = t[index++]) == null);
}
return e;
}
总结起来就是entrySet并未新建一个Map的备份或者拷贝,而是通过内部类直接对HashMap中table的访问来进行遍历和访问。