Map是一个映射键和值的对象。类似于Python'中的字典?
HashMap为什么会出现呢?因为数组这种数据结构,虽然遍历简单,但插入和删除操作复杂,需要移动数组内部元素;链表这种数据结构,插入和删除操作简单,但是查找复杂,只能一个一个地遍历。有没有一种新的数据结构,插入数据简单,同时查找也简单?这个时候就出现了哈希表这种数据结构。这是一种折中的方式,插入没链表快,查询没数组快。
wiki' 上就是这么定义哈希表的:
散列表(Hash table,也叫哈希表),是根据关键字(key value)而直接访问在内部存储位置的数据结构。也就是说,它通过计算一个关于键值的函数,将所需查询的数据映射到表中一个位置来访问记录,这加快了查询速度。这个映射函数称作散列函数,存放记录的数组称作散列表。
有几个概念要解释下:
(1).如果有一个关键字为 k ,它是通过一种函数 f(k) 得到散列表的地址,然后把值放在这个地址上。这个函数 f 就称为散列函数,也叫哈希函数。
(2)对于不同的关键字,得到了同一地址,即k1 != k2,但是f(k1)== f(k2)。这种现象称为冲突。
(3)若对于关键字集合中的任一个关键字,经散列函数映射到地址集合中任一个地址的概率是相等的,则称此类散列函数为均匀散列函数。
散列函数有好几种实现,分别有直接定址法、随机数法、除留余数法等,在wiki'散列表上都有介绍。
散列表的冲突解决方法也有好几种,有开放定址法、单独链表法、再散列等。
Java中的HashMap采用的冲突解决方法是使用单独链表法,如下图所示:
HashMap原理分析
HashMap是JDK中Map接口的实现类之一,是一个散列表的实现。
HahsMap中的 Key 和 Value 都可以为 null ,且它的方法都没有synchronized 。其他方法的实现大致跟 HashTable一致。
HashMap有个内部静态类Entry<K,V>(我的JDK是1.7.0_79,可能有的版本是Node<K,V>,@see 1.2),这个Entry<K,V>就是为了解决冲突而设计的链表中的节点概念。它有4个属性,hash表示哈希地址,key表示关键字,value表示值,next表示这个节点的下一个节点,是一个单向链表:
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
int hash;
/**
* Creates new entry.
*/
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
在分析HashMap源码之前,先看一个HashMap使用例子
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("java", 1);
map.put("golang", 2);
map.put("python", 3);
map.put("ruby", 4);
map.put("scala", 5);
上面的代码会生成下面这张哈希表:
至于为什么会生成这样的哈希表,在后面的源码分析的时候会讲解。
HashMap的属性
HashMap有几个重要的属性:
/**
* An empty table instance to share when the table is not inflated.
*/
static final Entry<?,?>[] EMPTY_TABLE = {};
/**
* The table, resized as necessary. Length MUST Always be a power of two.
*/
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE; //哈希表数组
/**
* The number of key-value mappings contained in this map.
*/
transient int size; //键值对个数
/**
* The next size value at which to resize (capacity * load factor).
* @serial
*/
// If table == EMPTY_TABLE then this is the initial capacity at which the
// table will be created when inflated.
int threshold; //阀值。 值 = 容量 * 加载因子
/**
* The load factor for the hash table.
*
* @serial
*/
final float loadFactor; //加载因子。 默认是0.75f
有两个重要的特性影响着HashMap的性能,分别是capacity(容量)和loadFactory(加载因子)。其中capacity表示哈希表bucket(桶,见桶式排序)的数量,HashMap的默认只是16( 1 << 4)。loadFactory加载因子表示当一个map填满达到了这个比例之后的bucket时候,和ArrayList一样,将会创建原来HashMap大小的两倍的bucket数组,来重新调整map的大小,并将原来的对象放入新的bucket数组中。这个过程也叫作“重哈希”。默认的loadFactory为0.75f。
HashMap的操作
分析一下HashMap在put()键值对的过程,是如何找到bucket的,遇到哈希冲突的时候是如何使用链表法的。
public V put(K key, V value) {
// 第一个参数就是关键字key的哈希值
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; // 哈希表是空的话,重新构建,进行扩容
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null); // 没有hash冲突的话,直接在对应位置上构造一个新的节点即可
else { // 如果哈希表当前位置上已经有节点的话,说明有hash冲突
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 { // 跟链表进行比较
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;
}
}
if (e != null) { // 如果找到了节点,说明关键字相同,进行覆盖操作,直接返回旧的关键字的值
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操作:
get操作关键点就是怎么从哈希表中取数据,理解put操作后,get方法很容易理解了:
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
getNode(hash, key)方法说明了如何在哈希表上取数据
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) { // 如果哈希表容量为0或者关键字没有命中,直接返回null
if (first.hash == hash && // 关键字命中的话比较第一个节点
((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;
}
hash过程和resize过程分析:
static final int hash(Object key) {
int h;
// 使用hashCode的值和hashCode的值无符号右移16位做异或操作
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
这段代码什么意思?我们以中文的那个demo为例,说明“Java”这个关键字如何找到对应的bucket的过程:
//定位bucket索引的最后操作。如果 n 为奇数,n-1 就是偶数,偶数的话转成二进制最后一位是0,相反如果是奇数,最后一位是1,这样产生的索引值将更均匀。
(n - 1) & hash
hashCode的高16位与低16位进行异或操作主要是设计者想了一个顾全大局的方法(综合考虑了速度、作用、质量)。
static final int tableSizeFor(int cap) {
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
阀值为6是因为之后进行resize操作的时候更新了阀值。
下图是一个HashMap扩容之后的效果图:
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) { // 如果老容量大于0,说明哈希表中已经有数据了,然后进行扩容
if (oldCap >= MAXIMUM_CAPACITY) { // 超过最大容量的话,不扩容
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && // 容量加倍
oldCap >= DEFAULT_INITIAL_CAPACITY) // 如果老的容量超过默认容量的话
newThr = oldThr << 1; // 阀值加倍
}
else if (oldThr > 0) // 根据thresold初始化数组
newCap = oldThr;
else { // 使用默认配置
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) { // 扩容之后进行rehash操作
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e; // 单节点扩容
else if (e instanceof TreeNode)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap); // 红黑树方式处理
else { // 链表扩容
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
HashMap注意的地方:
1、HashMap底层是个哈希表,使用拉链法解决冲突
2、HashMap内部存储的数据是无序的,这是因为HashMap内部的数组的下表是根据hash值算出来的
3、HashMap允许key为null
4、HashMap不是一个线程安全的类