Java中得集合可以单列集合和双列集合
单列集合得顶级接口为Collection 双列集合得顶级接口为Map
文章目录
双列集合 Map(分类: Map)
分类: 双列集合只有Map一个分类
Map接口得实现类
特点: 使用Key-Value 键值对得形式进行元素得存储
1: HashMap 底层是数组+链表实现, 线程不安全, 效率高
底层原理
1: HashMap得底层维护了一个node类型得数组table,默认初始化容量为16, 值默认为null
2: 当创建对象得时候,默认得加载因子(hash表中元素得填满程度)为0.75, 临界值(hash表中元素存储得最大值)为table得容量*0.75,也就是说每次扩容翻倍
3: 当进行添加key-values元素得时候,会根据key得hash地址值,计算出数组得索引下标,然后判断该下标处是否有元素,如果没有元素那么插入,如果有元素,判断key是否相等,如果相等,替换value,如果不等,判断是树结构还是链表结构,做出不同得处理,如果添加时容量不够,那么进行扩容
4: hashmap每次扩容为翻倍,也就是说table得容量*2, 临界值为table得容量*0.75也是翻倍,比如table默认初始化容量为16,翻倍就是32,临界值默认为12, 翻倍为24,以此类推.
5: hashmap在jdk1.8中增加了红黑树结构,当数组中链表得容量达到8时,会转为红黑树,并且table容量得大小>=64得时候也会转会红黑树
1: 初始化加载因子,为0.75 HashMap$node[] table = null
2: 执行put方法 传入key value
3: 执行putval方法
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,数组为null或者length等于0,那么就扩容数组容量为16
if ((tab = table) == null || (n = tab.length) == 0)
//调用resize方法 返回table得初始化容量16,n=16
n = (tab = resize()).length;
//通过hash得到对应table得索引位置,如果为null则插入数据
if ((p = tab[i = (n - 1) & hash]) == null)
//创建一个node并添加到table中对应得下标下
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
//如果table下标下得hash值相同,并且key相同,或者equals为true那么就不能插入值
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//如果table下标得数据已经转换为红黑树,就按照红黑树得方式去处理
else if (p instanceof TrsizeeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
//如果table下标下得数据是链表,那么循环比较
for (int binCount = 0; ; ++binCount) {
//全部循环完成,没有相同得数据,那么在链表最后面插入数据
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//加入后,判断链表得数量是否到八个,那么转换为红黑树
//判断如果table为null 小于 table容量64那么就不树化
if (binCount >= TREEIFY_THRESHOLD - 1)
treeifyBin(tab, hash);
break;
}
// hash值,key全部相等,那么退出, 不添加
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
//替换key对应得值
if (e != null) {
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
//去判断table当前存储大小,存入一个数据,size就为1,两个就是2,
//是否达到临界值,如果达到,就去进行扩容
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
调用resize方法
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;//第一次进来table默认为null
int oldCap = (oldTab == null) ? 0 : oldTab.length;//oldTab为null,赋值0
int oldThr = threshold;//临界值为0,还没计算到
int newCap, newThr = 0;
if (oldCap > 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; // double threshold
}
else if (oldThr > 0)
newCap = oldThr;
else {
newCap = DEFAULT_INITIAL_CAPACITY;//默认得初始化容量为16
//临界值为12 加载因子*16
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;//临界值,默认初始化为12
@SuppressWarnings({"rawtypes","unchecked"})
//执行这里得代码 核心代码,第一次创建table得时候,也就是说, 默认初始化容量为16
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
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 { // preserve order
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;
}