java中Set中添加mutable變量修改后contains返回false

背景

題目有點(diǎn)復(fù)雜......語文太差....貼上代碼吧

public static void containTest() {
        List<String> list = new ArrayList<>();
        list.add("a");
        Set<List<String>> set = new HashSet<>();
        set.add(list);
        System.out.println(set.contains(list));
        list.add("b");
        System.out.println(set.contains(list));
}

上面的代碼返回如下:

true
false

而對于如下代碼:

public static void sbContainTest() {
        StringBuilder sBuilder = new StringBuilder("a");
        Set set = new HashSet<>();
        set.add(sBuilder);
        System.out.println(set.contains(sBuilder));
        
        sBuilder.append("b");
        System.out.println(set.contains(sBuilder));
    }

返回的結(jié)果卻是:

true 
true

我們來通過查看源碼來了解這背后的原理.

分析

首先查看HashSet的源碼, 大致貼一下其內(nèi)部域以及幾個和本文相關(guān)的方法如下:

public  class HashSet<E>
        extends AbstractSet<E>
        implements Set<E>, Cloneable, java.io.Serializable{
        static final long serialVersionUID = -5024744406713321676L;
        private transient HashMap<E, Object> map;
        
        // Dummy value to associate with an Object in the backing Map
        private static final Object PRESENT = new Object();

        public HashSet(){
            map = new HashMap<>();
        }

        public boolean add(E e) {
            return map.put(e, PRESENT) == null;
        }

        public boolean contains(Object o) {
            return map.containsKey(o);
        }

        //other methods
}

可以看到HashSet內(nèi)部是使用一個HashMap存儲數(shù)據(jù)的, 而contains方法調(diào)用的是HashMapcontains方法.

我們再查看HashMap源碼, 看看contiansKey的實(shí)現(xiàn). 同上, 貼一些關(guān)鍵代碼

public class HashMap<K,V> extends AbstractMap<K,V>
        implements Map<K,V>, Cloneable, Serializable {
        private static final long serialVersionUID = 362498820763181265L;
        transient Node<K, V>[] table;
        transient int size;
        
        public boolean containsKey(Object key) {
            return getNode(hash(key), key) != null;
        }
        
        public V put(K key, V value) {
            return putVal(hash(key), key, value, false, true);
        }

        final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                       boolean evict) {
            Node<K,V>[] tab; Node<K,V> p; int n, i;
            if ((tab = table) == null || (n = tab.length) == 0)
                n = (tab = resize()).length;
            if ((p = tab[i = (n - 1) & hash]) == null)
                tab[i] = newNode(hash, key, value, null);    //look at this line!!!!!
            else {
                Node<K,V> e; K k;
                if (p.hash == hash &&
                    ((k = p.key) == key || (key != null && key.equals(k))))
                    e = p;
                else if (p instanceof TreeNode)
                    e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
                else {
                    for (int binCount = 0; ; ++binCount) {
                        if ((e = p.next) == null) {
                            p.next = newNode(hash, key, value, null);
                             if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                                treeifyBin(tab, hash);
                            break;
                        }
                        if (e.hash == hash &&
                            ((k = e.key) == key || (key != null && key.equals(k))))
                            break;
                        p = e;
                    }
                }
                if (e != null) { // existing mapping for key
                    V oldValue = e.value;
                    if (!onlyIfAbsent || oldValue == null)
                        e.value = value;
                    afterNodeAccess(e);
                    return oldValue;
                }
            }
            ++modCount;
            if (++size > threshold)
                resize();
            afterNodeInsertion(evict);
            return null;
        }
        /**
        *@param: hash hash for key, key the key
        *@return: the node, or null if none
        **/
        final Node<K, V> getNode(int hash, Object key) {
            Node<K, V>[] tab; 
            Node<K, V> first, e; 
            int n; 
            K k;
            if (( tab = table) != null && (n = tab.length) > 0 && (first = tab[(n-1) & hash]) != null) {
                if (first.hash == hash && ((k = first.key) == key || (key != null && key.equals(k)))) {
                    return first;
                }
                if ((e = first.next) != null) {
                    if (first isinstanceof TreeNode){
                        return ((TreeNode<K, V> first).getTreeNode(hash, key);
                    do {
                        if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) {
                            return e;
                        } while ((e = e.next) != null);
                }
            }
            return null
        }
        
        static final int hash(Object key) {
            int h;
            return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>>16);
        }

}
                

通過查看上面的代碼我們就知道原因了, 簡單地用語言描述一下

原因

我們運(yùn)行如下代碼:

public static void hashCodeTest() {
        List<String> list = new ArrayList<>();
        list.add("a");
        System.out.println(list.hashCode());
        list.add("b");
        System.out.println(list.hashCode());
        
        StringBuilder sBuilder = new StringBuilder("a");
        System.out.println(sBuilder.hashCode());
        sBuilder.append("b");
        System.out.println(sBuilder.hashCode());
    }

返回結(jié)果如下

128
4066
1094834071
1094834071

首先我們設(shè)list里面只有"a"時的hashcode的值為a, 添加了"b"之后的hashcode的值為b.
當(dāng)我們第一運(yùn)行set.add(list)時候
當(dāng)運(yùn)行set.add(list)時, 注意觀察上面我貼的HashMap的源碼第21行(我用look at this line!!!!!注釋了), 可知HashMap是根據(jù)此時的hashcode(也就是a)來確定list存儲在set中的索引.
而我們第二次運(yùn)行set.contains(list)時HashMap會用新的索引(也就是b)來尋找目標(biāo), 自然找不到了. 于是就返回了false.
而sBuilder的hashcode的值沒有改變, 自然也就能被找到, 返回true了.

收獲

以后使用和hash相關(guān)的數(shù)據(jù)結(jié)構(gòu)的時候一定要注意 hash值得變化, 變了可能就找不到了. hash固然快, 但是也是有缺點(diǎn)吶.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,628評論 18 399
  • 本文出自 Eddy Wiki ,轉(zhuǎn)載請注明出處:http://eddy.wiki/interview-java.h...
    eddy_wiki閱讀 1,223評論 0 16
  • 從三月份找實(shí)習(xí)到現(xiàn)在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂視家的研發(fā)崗...
    時芥藍(lán)閱讀 42,767評論 11 349
  • 1.let 由javavscript的變量作用域其實(shí)是函數(shù)的內(nèi)部,所以我們在for循環(huán)等語句塊中是無法定義具有局部...
    youngiyang_打碼少年閱讀 820評論 0 0
  • 生活改變了我,也改變了你,以前碰到自己不喜歡的吃的東西,就不會去動下筷子,如今,即使以前如何的不喜歡,也會去嘗試一...
    王光磊_滁州閱讀 269評論 1 2

友情鏈接更多精彩內(nèi)容