国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

JDK源碼之解讀hashMap 的put和get方法的實現(xiàn)原理

2019-11-10 18:16:40
字體:
供稿:網(wǎng)友
JDK源碼之解讀hashMap 的put和get方法的實現(xiàn)原理1,put             對于方法hashmap.put(K,V),首先是把k處理,通過hashcode方法處理得到K對應(yīng)的hash=hash(K).             再調(diào)用h & (length-1)得到數(shù)組下標(biāo)i. 最后調(diào)用createEntry(hash, key, value, i)方法,把hash,key,value存入table[i]一維數(shù)組中。源碼如下:             
 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;    }
 final int hash(Object k) {        int h = 0;        if (useAltHashing) {            if (k instanceof String) {                return sun.misc.Hashing.stringHash32((String) k);            }            h = hashSeed;        }        h ^= k.hashCode();        // This function ensures that hashCodes that differ only by        // constant multiples at each bit position have a bounded        // number of collisions (apPRoximately 8 at default load factor).        h ^= (h >>> 20) ^ (h >>> 12);        return h ^ (h >>> 7) ^ (h >>> 4);    }
 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);    }
總結(jié)就是說,先用鍵生成hashcode,然后把鍵和值存入一個對象為鍵值對的一維數(shù)組中,位置是,按生成hashcode轉(zhuǎn)變得到的數(shù)字作為一維數(shù)組的下標(biāo)。
有人會說,萬一生成的hashcode一樣咋辦?=== 因為他是把鍵值對存入一維數(shù)組中,鍵是唯一的,所以hashcode一樣時候,根據(jù)對象里面的鍵不同,一樣可以取出唯一對應(yīng)的值。
2,get           弄懂put方法原理,這個就很簡單了。根據(jù)對應(yīng)的鍵,找到hashcode,去一維數(shù)組根據(jù)hashcode生成的下標(biāo)去取對應(yīng)的值,           如果hashcode一樣,根據(jù)唯一鍵在一位數(shù)組里面取值。源碼如下:          
  public V get(Object key) {        if (key == null)            return getForNullKey();        Entry<K,V> entry = getEntry(key);        return null == entry ? null : entry.getValue();    }
  final Entry<K,V> getEntry(Object key) {        int hash = (key == null) ? 0 : hash(key);        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 != null && key.equals(k))))                return e;        }        return null;    }
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 徐水县| 合作市| 策勒县| 茌平县| 澄江县| 碌曲县| 宁晋县| 东兴市| 玉田县| 西城区| 高州市| 遂宁市| 咸宁市| 商城县| 鄱阳县| 江孜县| 边坝县| 建始县| 建宁县| 玉田县| 肥西县| 离岛区| 陕西省| 随州市| 阿合奇县| 祁连县| 资讯 | 绍兴市| 澜沧| 乌恰县| 满城县| SHOW| 桓仁| 英山县| 拜泉县| 花莲县| 山西省| 平舆县| 晋宁县| 安图县| 阳城县|