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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

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

2019-11-09 19:52:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
JDK源碼之解讀hashMap 的put和get方法的實(shí)現(xiàn)原理1,put             對(duì)于方法hashmap.put(K,V),首先是把k處理,通過(guò)hashcode方法處理得到K對(duì)應(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é)就是說(shuō),先用鍵生成hashcode,然后把鍵和值存入一個(gè)對(duì)象為鍵值對(duì)的一維數(shù)組中,位置是,按生成hashcode轉(zhuǎn)變得到的數(shù)字作為一維數(shù)組的下標(biāo)。
有人會(huì)說(shuō),萬(wàn)一生成的hashcode一樣咋辦?=== 因?yàn)樗前焰I值對(duì)存入一維數(shù)組中,鍵是唯一的,所以hashcode一樣時(shí)候,根據(jù)對(duì)象里面的鍵不同,一樣可以取出唯一對(duì)應(yīng)的值。
2,get           弄懂put方法原理,這個(gè)就很簡(jiǎn)單了。根據(jù)對(duì)應(yīng)的鍵,找到hashcode,去一維數(shù)組根據(jù)hashcode生成的下標(biāo)去取對(duì)應(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ā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 察隅县| 龙游县| 喀喇| 灵武市| 内江市| 永修县| 东港市| 阜宁县| 新余市| 杭锦后旗| 聂荣县| 交城县| 雷山县| 恭城| 新野县| 和平区| 精河县| 武胜县| 吴江市| 策勒县| 牟定县| 漳浦县| 永川市| 巴林左旗| 尤溪县| 广东省| 公安县| 洛宁县| 永康市| 柳林县| 庆阳市| 靖远县| 巴彦县| 呼图壁县| 河间市| 定结县| 五指山市| 横峰县| 彭州市| 库车县| 黑龙江省|