1.采用数组 + 链表/红黑树结构,链表元素大于等于8时转为红黑树,树元素个数降到6时转为链表
//数组
transient Node<K,V>[] table;
//链表
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
}
//红黑数
static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
TreeNode<K,V> parent; // red-black tree links
TreeNode<K,V> left;
TreeNode<K,V> right;
TreeNode<K,V> prev; // needed to unlink next upon deletion
boolean red;
}
2.对key取得哈希值后,用其高16位与低16位进行异或运算,最终的到结果跟(数组长度-1)进行与运算,这样来获得数组下标
3.扩容时,用一个高位链表和一个低位链表存元素,再插入到新数组
HashMap - jdk1.8
最新推荐文章于 2023-02-22 15:18:22 发布