HashMap學(xué)習(xí)

概述

  1. 線程非安全团滥,并且允許key與value都為null值颓影,HashTable與之相反,為線程安全盾鳞,key與value都不允許null值犬性。
  2. put、get操作的時(shí)間復(fù)雜度為O(1)腾仅。
  3. 不保證其內(nèi)部元素的順序乒裆,而且隨著時(shí)間的推移,同一元素的位置也可能改變(resize的情況)
  4. 遍歷其集合視角的時(shí)間復(fù)雜度與其容量(capacity攒砖,槽的個(gè)數(shù))和現(xiàn)有元素的大懈淄谩(entry的個(gè)數(shù))成正比
  5. Map m = Collections.synchronizedMap(new HashMap(...)); 通過這種方式可以得到一個(gè)線程安全的map日裙。

數(shù)據(jù)結(jié)構(gòu)

哈希表:數(shù)組+鏈表

markdown-img-paste-20161128182007235.png

定義

簽名

public class HashMap<K,V>
    extends AbstractMap<K,V> //AbstractMap類提供Map接口的基本實(shí)現(xiàn)
    implements Map<K,V>, Cloneable, Serializable//Map接口定義了鍵映射到值的規(guī)則

哈希函數(shù)的設(shè)計(jì)

final int hash(Object k) {
        int h = hashSeed;
        if (0 != h && k instanceof String) {
            return sun.misc.Hashing.stringHash32((String) k);
        }

        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);
    }

get操作

  public V get(Object key) {
        if (key == null)
            return getForNullKey();
        Entry<K,V> entry = getEntry(key);

        return null == entry ? null : entry.getValue();
    }

    private V getForNullKey() {
        if (size == 0) {
            return null;
        }
        // Null keys map to index 0,遍歷單向鏈表
        for (Entry<K,V> e = table[0]; e != null; e = e.next) {
            if (e.key == null)
                return e.value;
        }
        return null;
    }

    final Entry<K,V> getEntry(Object key) {
        // 根據(jù)key尋找entry,類似于給下表惰蜜,在數(shù)組中尋找
        if (size == 0) {
            return null;
        }

        int hash = (key == null) ? 0 : hash(key);
        for (Entry<K,V> e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            Object k;
            // 相等條件:hash值相等且key相等(==或者equal相等)
            if (e.hash == hash &&
                ((k = e.key) == key || (key != null && key.equals(k))))
                return e;
        }
        return null;
    }

containsKey操作

  public boolean containsKey(Object key) {
      return getEntry(key) != null;
  }

put操作

  public V put(K key, V value) {
        if (table == EMPTY_TABLE) {
            inflateTable(threshold);
        }
        // key為null時(shí)的put操作
        if (key == null)
            return putForNullKey(value);
        // key不為null時(shí)put操作:先查看key是否存在昂拂,存在則更新
        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;
            }
        }

        // 不存在則新加一條entry,modCount++
        modCount++;
        addEntry(hash, key, value, i);
        return null;
    }

    /**
     * Offloaded version of put for null keys
     */
    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;
    }

    /**
     * This method is used instead of put by constructors and
     * pseudoconstructors (clone, readObject).  It does not resize the table,
     * check for comodification, etc.  It calls createEntry rather than
     * addEntry.
     */
    private void putForCreate(K key, V value) {
        int hash = null == key ? 0 : hash(key);
        int i = indexFor(hash, table.length);

        /**
         * Look for preexisting entry for key.  This will never happen for
         * clone or deserialize.  It will only happen for construction if the
         * input Map is a sorted map whose ordering is inconsistent w/ equals.
         */
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash &&
                ((k = e.key) == key || (key != null && key.equals(k)))) {
                e.value = value;
                return;
            }
        }

        createEntry(hash, key, value, i);
    }

    private void putAllForCreate(Map<? extends K, ? extends V> m) {
        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
            putForCreate(e.getKey(), e.getValue());
    }



    // put一個(gè)map集合的操作
    public void putAll(Map<? extends K, ? extends V> m) {
        int numKeysToBeAdded = m.size();
        if (numKeysToBeAdded == 0)
            return;

        if (table == EMPTY_TABLE) {
            inflateTable((int) Math.max(numKeysToBeAdded * loadFactor, threshold));
        }

        /*
         * Expand the map if the map if the number of mappings to be added
         * is greater than or equal to threshold.  This is conservative; the
         * obvious condition is (m.size() + size) >= threshold, but this
         * condition could result in a map with twice the appropriate capacity,
         * if the keys to be added overlap with the keys already in this map.
         * By using the conservative calculation, we subject ourself
         * to at most one extra resize.
         */
        if (numKeysToBeAdded > threshold) {
            int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
            if (targetCapacity > MAXIMUM_CAPACITY)
                targetCapacity = MAXIMUM_CAPACITY;
            int newCapacity = table.length;
            while (newCapacity < targetCapacity)
                newCapacity <<= 1;
            if (newCapacity > table.length)
                resize(newCapacity);
        }

        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
            put(e.getKey(), e.getValue());
    }

resize操作


void resize(int newCapacity) {
    Entry[] oldTable = table;
    int oldCapacity = oldTable.length;
    // capacity已經(jīng)最大抛猖,則將threshold設(shè)置為最大值
    if (oldCapacity == MAXIMUM_CAPACITY) {
        threshold = Integer.MAX_VALUE;
        return;
    }
    // 構(gòu)造新的數(shù)組
    Entry[] newTable = new Entry[newCapacity];
    // 將舊數(shù)組中的數(shù)據(jù)轉(zhuǎn)移到新數(shù)組中
    transfer(newTable, initHashSeedAsNeeded(newCapacity));
    table = newTable;
    // 更新threshold
    threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}

/**
 * Transfers all entries from current table to newTable.
 */
void transfer(Entry[] newTable, boolean rehash) {
    int newCapacity = newTable.length;
    // 遍歷舊數(shù)組當(dāng)中的每一條entry
    for (Entry<K,V> e : table) {
        while(null != e) {
            Entry<K,V> next = e.next;
            if (rehash) {
                // 在新數(shù)組當(dāng)中的hash值
                e.hash = null == e.key ? 0 : hash(e.key);
            }
            // 重新計(jì)算hash值后在新數(shù)組中的下表
            int i = indexFor(e.hash, newCapacity);

            e.next = newTable[i];
            newTable[i] = e;
            e = next;
        }
    }
}

hash函數(shù)h & (length-1)詳解
由于length是2的冪格侯,所以length-1每一位都是1,相當(dāng)于取余操作财著,且碰撞的幾率小联四。如果有0,則與的時(shí)候可能不同的數(shù)會(huì)發(fā)生哈希碰撞撑教。同時(shí)朝墩,如果與的時(shí)候有0,那么與的結(jié)果一定是0伟姐,則該位置為1的一些地址始終會(huì)用不到收苏,造成空間浪費(fèi)!

remove操作

  public V remove(Object key) {
        Entry<K,V> e = removeEntryForKey(key);
        return (e == null ? null : e.value);
    }

    /**
     * Removes and returns the entry associated with the specified key
     * in the HashMap.  Returns null if the HashMap contains no mapping
     * for this key.
     */
    final Entry<K,V> removeEntryForKey(Object key) {
        if (size == 0) {
            return null;
        }
        int hash = (key == null) ? 0 : hash(key);
        int i = indexFor(hash, table.length);
        // 該索引的第一個(gè)節(jié)點(diǎn)
        Entry<K,V> prev = table[i];
        Entry<K,V> e = prev;

        while (e != null) {
            Entry<K,V> next = e.next;
            Object k;
            if (e.hash == hash &&
                ((k = e.key) == key || (key != null && key.equals(k)))) {
                // 找到要?jiǎng)h除的key
                modCount++;
                size--;
                if (prev == e)
                    // 如果第一個(gè)就是要?jiǎng)h除的節(jié)點(diǎn)愤兵,直接將下一個(gè)節(jié)點(diǎn)放到table[i]
                    table[i] = next;
                else
                    // 否則將上一個(gè)節(jié)點(diǎn)的next指向要?jiǎng)h除的下一個(gè)節(jié)點(diǎn)
                    prev.next = next;
                e.recordRemoval(this);
                return e;
            }
            // 沒有找到鹿霸,向后循環(huán)查找
            prev = e;
            e = next;
        }

        return e;
    }

clear操作

    // 將table的每一個(gè)位置置為null,注意并不會(huì)重置capacity
    public void clear() {
        modCount++;
        Arrays.fill(table, null);
        size = 0;
    }

containsKey/containsValue操作

    public boolean containsKey(Object key) {
        return getEntry(key) != null;
    }

    public boolean containsValue(Object value) {
        if (value == null)
            return containsNullValue();

        Entry[] tab = table;
        for (int i = 0; i < tab.length ; i++)
            for (Entry e = tab[i] ; e != null ; e = e.next)
                if (value.equals(e.value))
                    return true;
        return false;
    }


    private boolean containsNullValue() {
        Entry[] tab = table;
        for (int i = 0; i < tab.length ; i++)
            for (Entry e = tab[i] ; e != null ; e = e.next)
                if (e.value == null)
                    return true;
        return false;
    }

單項(xiàng)鏈表entry

        static class Entry<K,V> implements Map.Entry<K,V> {
        // 四個(gè)成員變量秆乳,應(yīng)該牢記
        final K key;
        V value;
        Entry<K,V> next;
        int hash;

        /**
         * Creates new entry.
         */
        Entry(int h, K k, V v, Entry<K,V> n) {
            value = v;
            next = n;
            key = k;
            hash = h;
        }

        public final K getKey() {
            return key;
        }

        public final V getValue() {
            return value;
        }

        public final V setValue(V newValue) {
            V oldValue = value;
            value = newValue;
            return oldValue;
        }

        public final boolean equals(Object o) {
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry e = (Map.Entry)o;
            Object k1 = getKey();
            Object k2 = e.getKey();
            if (k1 == k2 || (k1 != null && k1.equals(k2))) {
                Object v1 = getValue();
                Object v2 = e.getValue();
                if (v1 == v2 || (v1 != null && v1.equals(v2)))
                    return true;
            }
            return false;
        }

        public final int hashCode() {
            return Objects.hashCode(getKey()) ^ Objects.hashCode(getValue());
        }

        public final String toString() {
            return getKey() + "=" + getValue();
        }

        /**
         * This method is invoked whenever the value in an entry is
         * overwritten by an invocation of put(k,v) for a key k that's already
         * in the HashMap.
         */
        void recordAccess(HashMap<K,V> m) {
        }

        /**
         * This method is invoked whenever the entry is
         * removed from the table.
         */
        void recordRemoval(HashMap<K,V> m) {
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末懦鼠,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子屹堰,更是在濱河造成了極大的恐慌肛冶,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,110評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件双藕,死亡現(xiàn)場離奇詭異淑趾,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)忧陪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門扣泊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人嘶摊,你說我怎么就攤上這事延蟹。” “怎么了叶堆?”我有些...
    開封第一講書人閱讀 165,474評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵阱飘,是天一觀的道長。 經(jīng)常有香客問我,道長沥匈,這世上最難降的妖魔是什么蔗喂? 我笑而不...
    開封第一講書人閱讀 58,881評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮高帖,結(jié)果婚禮上缰儿,老公的妹妹穿的比我還像新娘。我一直安慰自己散址,他們只是感情好乖阵,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,902評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著预麸,像睡著了一般瞪浸。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上吏祸,一...
    開封第一講書人閱讀 51,698評(píng)論 1 305
  • 那天对蒲,我揣著相機(jī)與錄音,去河邊找鬼犁罩。 笑死齐蔽,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的床估。 我是一名探鬼主播,決...
    沈念sama閱讀 40,418評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼诱渤,長吁一口氣:“原來是場噩夢啊……” “哼丐巫!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起勺美,我...
    開封第一講書人閱讀 39,332評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤递胧,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后赡茸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體缎脾,經(jīng)...
    沈念sama閱讀 45,796評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,968評(píng)論 3 337
  • 正文 我和宋清朗相戀三年占卧,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了遗菠。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,110評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡华蜒,死狀恐怖辙纬,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情叭喜,我是刑警寧澤贺拣,帶...
    沈念sama閱讀 35,792評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響譬涡,放射性物質(zhì)發(fā)生泄漏闪幽。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,455評(píng)論 3 331
  • 文/蒙蒙 一涡匀、第九天 我趴在偏房一處隱蔽的房頂上張望盯腌。 院中可真熱鬧,春花似錦渊跋、人聲如沸腊嗡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽燕少。三九已至,卻和暖如春蒿囤,著一層夾襖步出監(jiān)牢的瞬間客们,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評(píng)論 1 272
  • 我被黑心中介騙來泰國打工材诽, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留底挫,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,348評(píng)論 3 373
  • 正文 我出身青樓脸侥,卻偏偏與公主長得像建邓,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子睁枕,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,047評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 先說說HashMap的幾個(gè)特點(diǎn): 1官边、無序的(那它的存取速度咋還那么快呢?) 2外遇、線程不安全的(存取不同步) 第二...
    晴川落雨閱讀 341評(píng)論 0 0
  • HashMap 是 Java 面試必考的知識(shí)點(diǎn)注簿,面試官從這個(gè)小知識(shí)點(diǎn)就可以了解我們對(duì) Java 基礎(chǔ)的掌握程度。網(wǎng)...
    野狗子嗷嗷嗷閱讀 6,667評(píng)論 9 107
  • HashMap :基于哈希表的Map接口實(shí)現(xiàn)跳仿。底層基于數(shù)組诡渴,鏈表,1.8引入紅黑樹菲语。 主要屬性 final flo...
    聚在散里閱讀 424評(píng)論 0 0
  • 一.135編輯器 1.操作簡單妄辩,樣式豐富多樣,頁面簡潔大方谨究。 2.有一鍵排版功能恩袱,更方便快捷。 3.有配色方案設(shè)置...
    HLF1254閱讀 399評(píng)論 1 0
  • 文/孤鳥差魚 媽媽說 這么大的人了 別哭了 你抹了抹眼淚 搖了搖她的衣角 問她要了顆糖
    孤鳥差魚閱讀 195評(píng)論 1 3