亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java - hashmap put方法問(wèn)題

瀏覽:82日期:2023-10-31 10:36:24

問(wèn)題描述

public V put(K key, V value) {if (key == null) return putForNullKey(value);int hash = hash(key);int i = indexFor(hash, table.length);for (Entry<K,V> e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {V oldValue = e.value;e.value = value;e.recordAccess(this);return oldValue; }}modCount++;addEntry(hash, key, value, i);return null; }

void addEntry(int hash, K key, V value, int bucketIndex) { if ((size >= threshold) && (null != table[bucketIndex])) {resize(2 * table.length);hash = (null != key) ? hash(key) : 0;bucketIndex = indexFor(hash, table.length); } createEntry(hash, key, value, bucketIndex);}

void createEntry(int hash, K key, V value, int bucketIndex) {Entry<K,V> e = table[bucketIndex];table[bucketIndex] = new Entry<>(hash, key, value, e);size++; }

如果put 鍵索引相同key不同的元素,addEntry()怎么保存元素到鏈表尾部,createEntry()方法?

問(wèn)題解答

回答1:

看這段源碼

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 keyV oldValue = e.value;if (!onlyIfAbsent || oldValue == null) e.value = value;afterNodeAccess(e);return oldValue; }}++modCount;if (++size > threshold) resize();afterNodeInsertion(evict);return null; }

如果相同key,不同value會(huì)進(jìn)入這段:

if (e != null) { // existing mapping for keyV oldValue = e.value;if (!onlyIfAbsent || oldValue == null) e.value = value;afterNodeAccess(e);return oldValue; }

標(biāo)簽: java
相關(guān)文章:
主站蜘蛛池模板: 日韩黄色在线观看 | 一级毛片免费高清视频 | 91精品国产高清在线入口 | 免费黄网址 | 久久久国产精品福利免费 | 午夜高清性色生活片 | 亚洲国产日韩欧美在线a乱码 | 午夜三级在线 | 久久久男女野外野战 | 你懂的国产精品 | 欧美国产成人免费观看永久视频 | 99久久精品国产一区二区 | 成人国产精品一级毛片了 | 亚洲国产精品婷婷久久久久 | 国产精品美女在线 | 91久久国产情侣真实对白 | 国产爽妇网| 三级黄色片在线观看 | 免费观看一级成人毛片 | 亚洲网站大全 | 欧美片欧美日韩国产综合片 | 亚洲大片| 在线观看亚洲一区 | 黄色免费一级视频 | 二区三区不卡不卡视频 | 日本高清免费一本视频在线观看 | 色琪琪综合网站 | 成人精品国产亚洲 | 国产成人综合高清在线观看 | 国产一区二区视频免费 | 久久亚洲综合色 | 在线免费观看网址 | 国产一区二区在线免费观看 | 男女爱爱免费 | 午夜亚洲精品久久久久久 | 国产高清在线精品一区 | 欧美日韩国产一区二区三区播放 | 欧美大片国产在线永久播放 | 久久综合精品国产一区二区三区无 | 亚洲在线视频观看 | 亚洲欧美中文日韩二区一区 |