HashMap底层原理分析(put、get方法)
生活随笔
收集整理的這篇文章主要介紹了
HashMap底层原理分析(put、get方法)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
1、HashMap底層原理分析(put、get方法)
HashMap底層是通過數組加鏈表的結構來實現的。HashMap通過計算key的hashCode來計算hash值,只要hashCode一樣,那hash值就是相同的。當hash值相同時,就會出現hash沖突,HashMap通過鏈表來解決沖突。
原理圖:
?
實例:
import java.util.HashMap; import java.util.Map; ? public class HashMapTest {public static void main(String[] args) {Map<String, String> map = new HashMap<>();map.put("fruit", "apple");System.out.println(map.get("fruit"));?
1、put方法分析
//添加鍵值對方法 public V put(K key, V value) {//如果key為null,則hash值為0,將這個entry放在索引為0的地方,即table[0]if (key == null)return putForNullKey(value);//key不為nullint hash = hash(key.hashCode());//計算key的hashCode的hash值int i = indexFor(hash, table.length);//返回hash值所在的索引位置//遍歷table[i]處的Entry,若存在key相同并且hash值相同,則用新元素替換舊元素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;}}//修改標記+1,然后進行元素添加操作modCount++;addEntry(hash, key, value, i);//將包含特定key、value和hash值的entry添加到特定的桶中return null;}//添加key為null的元素private V putForNullKey(V value) {for (Entry<K,V> e = table[0]; e != null; e = e.next) {if (e.key == null) {V oldValue = e.value;e.value = value;e.recordAccess(this);return oldValue;}}modCount++;addEntry(0, null, value, 0);return null;}/*** Adds a new entry with the specified key, value and hash code to* the specified bucket. It is the responsibility of this* method to resize the table if appropriate.** Subclass overrides this to alter the behavior of put method.*/void addEntry(int hash, K key, V value, int bucketIndex) {Entry<K,V> e = table[bucketIndex];table[bucketIndex] = new Entry<>(hash, key, value, e);if (size++ >= threshold)resize(2 * table.length);}?
2、get方法分析
/** * 返回映射的key對應的value值,若map不包含該key,返回null */ public V get(Object key) {if (key == null)//獲取key為null的value值return getForNullKey();int hash = hash(key.hashCode());//計算key的hashCode的hash值//遍歷數組中對應hash值的Entry鏈表,若Entry元素的hash值與hashCode的hash值相等并且key與查找的key相等,則返回此Entry元素的value值for (Entry<K,V> e = table[indexFor(hash, table.length)];e != null;e = e.next) {Object k;if (e.hash == hash && ((k = e.key) == key || key.equals(k)))return e.value;}return null;}?
轉載于:https://www.cnblogs.com/stm32stm32/p/9035519.html
總結
以上是生活随笔為你收集整理的HashMap底层原理分析(put、get方法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python删除文件及进行文件夹压缩
- 下一篇: 清结算系统的一些思考