HashMap
Hashmap是java集合中,比较重要的一种集合框架.
特点:键值对存储,key可以为null,查找速度快.但是线程不安全.
hashMap的类关系
public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {
Map
hashMap的数据结构
在jdk1.6中,HashMap采用table数组(位桶)+单向链表实现.
突,同一hash值的链表都存储在一个链表里。但是当位于一个桶中的元素较多,即hash值相等的元素较多时,通过key值依次查找的效率较低。
而JDK1.8中,HashMap采用位桶+链表+红黑树实现,当链表长度超过阈值(8)时,将链表转换为红黑树,这样大大减少了查找时间。
算法实现
1.首先是hash算法
public native int hashCode();本地方法 实现
static final int hash(Object key) {
int h;
//无符号右移,高位补0
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
2.node节点
static class Node<K,V> implements Map.Entry<K,V> {
final int hash; //hash hash for key
final K key; //key
V value; //value
Node<K,V> next;//单向链表
3.插入算法
当key为空时,jdk1.6的方法是putForNullKey,在1.8中暂没找到策略
判断键值对数组tab[]是否为空或为null,否则resize();
根据键值key计算hash值得到插入的数组索引i,如果tab[i]==null,直接新建节点添加,否则转入3
判断当前数组中处理hash冲突的方式为链表还是红黑树(check第一个节点类型即可),分别处理。
public V put(K key, V value) {
//第一次hash计算,来确定table的位置
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;
//如果tab为空或长度为0,则分配内存resize()
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//(n - 1) & hash找到put位置,如果为空,则直接put
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//红黑树和AVL树类似,都是在进行插入和删除操作时通过特定操作保持二叉查找树的平衡,从而获得较高的查找性能。
//属于红黑树处理冲突
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
//链表处理冲突
for (int binCount = 0; ; ++binCount) {
//p第一次指向表头,以后依次后移
if ((e = p.next) == null) {
//e为空,表示已到表尾也没有找到key值相同节点,则新建节点
p.next = newNode(hash, key, value, null);
//新增节点后如果节点个数到达阈值,则将链表转换为红黑树 ,让各节点的路径平衡,
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//容许null==null
if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;//更新p指向下一个节点
}
}
//更新hash值和key值均相同的节点Value值
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;
}
查找
先找到index,
//再去找对应Key
//再找到 object
*
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;
//hash & (length-1)得到对象的保存位,
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 {//如果是链表,则遍历此table下的key
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
*
*/