日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HashMap的put方法返回值问题

發布時間:2025/3/12 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HashMap的put方法返回值问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

API文檔中的描述:

先看一個例子

Map<Character, Integer> map = new HashMap<Character, Integer>(); System.out.println(map.put('a', 0)); // null System.out.println(map.put('a', 1)); // 0 System.out.println(map.put('a', 2)); // 1 System.out.println(map.put('b', 1)); // null System.out.println(map.put('b', 2)); // 1 System.out.println(map.get('a')); //2

可以看出:put方法的返回值為null或value;

調用put方法時,如果已經存在一個相同的key, 則返回的是前一個key對應的value,同時該key的新value覆蓋舊value;
如果是新的一個key,則返回的是null;

通過hashmap的源碼可以看出:

map中一個映射不能包含重復的鍵。每個鍵最多只能映射一個值。即相同的key在Map中只會有一個與之關聯的value存在。
put()方法實現:首先使用hash(key)得到key的hashcode(),hashmap根據獲得的hashcode找到要插入的位置所在的鏈,在這個鏈里面放的都是hashcode相同的Entry鍵值對,
在找到這個鏈之后,會通過equals()方法判斷是否已經存在要插入的鍵值對,而這個equals比較的就是key。
添加對應的key-value這樣的鍵值對node時,如果原本已經存在相同的key,則直接改變對應的value,并返回舊的value;如果不存在相同的key,則插入,在插入鏈表時,如果鏈表長度為臨界長度TREEIFY_THRESHOLD,再插入任何元素就要變成紅黑樹。

public V put(K key, V value) {return putVal(hash(key), key, value, false, true);//put方法調用putVal方法}/*** Implements Map.put and related methods** @param hash hash for key* @param key the key* @param value the value to put* @param onlyIfAbsent if true, don't change existing value* @param evict if false, the table is in creation mode.* @return previous value, or null if none*/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 1sttreeifyBin(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 //key已經存在V oldValue = e.value; //將已經存在的結點e的value值賦給oldValueif (!onlyIfAbsent || oldValue == null)//當onlyIfAbsent為false或者oldValue為null時,進行覆蓋操作e.value = value;//覆蓋原結點的value值,用新值替換舊值afterNodeAccess(e);return oldValue;//返回的是被覆蓋的oldValue}}++modCount;if (++size > threshold)resize();afterNodeInsertion(evict);return null;}

在hashset中的add方法調用的就是hashmap的put方法,判斷put方法的返回值是否等于空,來保證hashset的值不重復。

private transient HashMap<E,Object> map;/*** Adds the specified element to this set if it is not already present.* More formally, adds the specified element <tt>e</tt> to this set if* this set contains no element <tt>e2</tt> such that* <tt>(e==null ? e2==null : e.equals(e2))</tt>.* If this set already contains the element, the call leaves the set* unchanged and returns <tt>false</tt>.** @param e element to be added to this set* @return <tt>true</tt> if this set did not already contain the specified* element*/public boolean add(E e) {return map.put(e, PRESENT)==null;}

總結

以上是生活随笔為你收集整理的HashMap的put方法返回值问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。