当前位置 : 主页 > 网络编程 > 其它编程 >

JDK1.8HashMap的resize()源码分析

来源:互联网 收集:自由互联 发布时间:2023-07-02
finalNode[]resize(){Node[]oldTabtable;intoldCap(oldTabnull)?0:oldTab.length;into final Node[] resize() { Node[] oldTab = table; int oldCap = (oldTab == null) ? 0 : oldTab.length; int oldThr = threshold; int newCap, newThr = 0; //如果有容量
finalNode[]resize(){Node[]oldTabtable;intoldCap(oldTabnull)?0:oldTab.length;into

final Node[] resize() { Node[] oldTab = table; int oldCap = (oldTab == null) ? 0 : oldTab.length; int oldThr = threshold; int newCap, newThr = 0; //如果有容量,说明该map已经有元素 if (oldCap > 0) { if (oldCap >= MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return oldTab; } //在此处newCap = oldCap <<1,容量翻倍了 else if ((newCap = oldCap <<1) 0) // initial capacity was placed in threshold newCap = oldThr; else { // zero initial threshold signifies using defaults newCap = DEFAULT_INITIAL_CAPACITY; newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); } if (newThr == 0) { float ft = (float)newCap * loadFactor; newThr = (newCap = DEFAULT_INITIAL_CAPACITY了,那么threshold也扩大两倍 */ if (oldTab != null) { //将原来map中非null的元素rehash之后再放到newTab里面去 for (int j = 0; j

上一篇:typeScript定义报错
下一篇:没有了
网友评论