一 :特点:
1.HashMap根据hashCode值存储数据,具有很快的访问速度
2.HashMap是非线性安全的
3.插入和删除数据效率比较高
二:组成:
数组+(链表或红黑树)
1.HashMap是一个数组
2.数组中每一个元素是一个单向链表
3.Java8中引入了红黑树
查找的时候根据hash值能够快速定位到数组的下标,之后需要链表一个一个比较下去才能找到需要的,时间复杂度取决于链表的长度及要找的数据的位置,为O(n)。为了减少时间复杂度,当链表中的元素超过8个以后,会将链表转化为红黑树(时间复杂度为O(logN))
三:HashMap中的put()和get()实现原理
1.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;
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;
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) { // 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;
}
一:调用hash函数将key转化为Hash值
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
二:将hash,key, value放到Node对象中
三:放入第一个元素时table为空,触发resize方法,初始化数组
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
四:n是数组的长度,不管hash的值是什么,经过(n - 1) & hash计算出来的i 的值一定在n-1之间。刚好是底层数组的合法下标,用i这个下标值去底层数组里去取值,如果为null,创建一个Node放到数组下标为i的位置,否则向里面添加元素。
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
2.get()
1.先调用k的hashCode()方法得出哈希值
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
2.如果table中有元素往下执行,否则返回null
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) {
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.如果要查找的元素是第一个则返回第一个元素
4.判断是否红黑树,是则红黑树中查找,否则do while循环遍历
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);
}
四 :红黑树基础
1.红黑树是一种近似平衡的二叉查找树,其主要的优点就是“平衡“,即左右子树高度几乎一致,以此来防止树退化为链表,通过这种方式来保障查找的时间复杂度为 log(n),而链表查询的复杂度为O(n),HashMap引入红黑树为了防止链表过长影响查询速度。
2.红黑树并不是完全平衡,在与平衡二叉树的时间复杂度相差不大情况下,保证每次插入最多只需要三次旋转就能达到平衡,实现起来更为简单,而平衡二叉树实现起来过于复杂。
3.每个节点必须只能是红色或者黑色,跟节点必须是黑色。
4.红色的节点,叶节点只能是黑色。
5.从任一节点到其每个叶子的所有路径都包含相同数目的黑色节点。