java - HashMap中afterNodeInsertion方法有什么作用呢
問題描述
環境:jdk1.8問題:學習HashMap的時候發現在putVal方法的最后調用了afterNodeInsertion方法
... ++modCount; if (++size > threshold)resize(); afterNodeInsertion(evict); return null;
又去搜索一下afterNodeInsertion方法,發現不少地方都調用了它,但是它的實現卻是
void afterNodeInsertion(boolean evict) { }
一個空方法??想知道這個方法到底有什么作用呢?
問題解答
回答1:// Callbacks to allow LinkedHashMap post-actionsvoid afterNodeAccess(Node<K,V> p) { }void afterNodeInsertion(boolean evict) { }void afterNodeRemoval(Node<K,V> p) { }
源碼中其實已經說了,這個三個方法都是為了繼承HashMap的LinkedHashMap類服務的。
LinkedHashMap 是 HashMap 的一個子類,它保留插入的順序,如果需要輸出的順序和輸入時的相同,那么就選用 LinkedHashMap。
LinkedHashMap中被覆蓋的afterNodeInsertion方法,用來回調移除最早放入Map的對象
void afterNodeInsertion(boolean evict) { // possibly remove eldest LinkedHashMap.Entry<K,V> first; if (evict && (first = head) != null && removeEldestEntry(first)) {K key = first.key;removeNode(hash(key), key, null, false, true); }}
相關文章:
1. 運行python程序時出現“應用程序發生異常”的內存錯誤?2. spring-mvc - spring-session-redis HttpSessionListener失效3. 利用IPMI遠程安裝centos報錯!4. IOS app應用軟件的id號怎么查詢?比如百度貼吧的app-id=4779278135. javascript - 最近用echarts做統計圖時遇到兩個問題!!6. javascript - node得到req不能得到boolean7. 淺談Vue使用Cascader級聯選擇器數據回顯中的坑8. java - Spring boot 讀取 放在 jar 包外的,log4j 配置文件,系統有創建日志文件,不寫入日志信息。9. javascript - 在 model里定義的 引用表模型時,model為undefined。10. mysql - 查詢 修改數據庫優化問題吧

網公網安備