久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

更新 Java HashMap 鍵

Updating Java HashMap key(更新 Java HashMap 鍵)
本文介紹了更新 Java HashMap 鍵的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我只是想知道,如果 HashMap 的鍵是可變的會(huì)發(fā)生什么,下面的測(cè)試程序證明了這一點(diǎn),我無法理解 equals 和 hashCode 方法何時(shí)返回true 和相同的值,為什么 hashmap.containsKey 返回 false.

I was just wondering, what would happen if key of a HashMap is mutable, test program below demonstrate that and I am unable to understand when both equals and hashCode methods returns true and same value, why does hashmap.containsKey return false.

public class MutableKeyHashMap {

    public static void main(String []a){

            HashMap<Mutable, String> map = new HashMap<Mutable, String>();
            Mutable m1 = new Mutable(5);
            map.put(m1, "m1");
            Mutable m2 = new Mutable(5);
            System.out.println(map.containsKey(m2));    

            m2.setA(6);
            m1.setA(6);
            Mutable m3 = map.keySet().iterator().next();

            System.out.println(map.containsKey(m2)+"    "+m3.hashCode()+"       "+m2.hashCode()+"       "+m3.equals(m2));   

    }
}
class Mutable {

    int a;

    public Mutable(int a) {

        this.a = a;
    }

    @Override
    public boolean equals(Object obj) {

        Mutable m = (Mutable) obj;
        return m.a == this.a ? true : false; 
    }

    @Override
    public int hashCode(){
        return a;
    }

    public void setA(int a) {

        this.a = a;
    }

    public int getA() {
        return a;
    }
} 

這是輸出:

真假 6 6 真

推薦答案

javadoc 解釋它

注意:如果將可變對(duì)象用作映射鍵,則必須非常小心.如果對(duì)象的值以影響等于比較的方式更改,而對(duì)象是映射中的鍵,則不會(huì)指定映射的行為.

Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map.

基本上,不要在 Map 中使用可變對(duì)象作為鍵,否則會(huì)被燒毀

Basically, don't use mutable objects as keys in a Map, you're going to get burnt

推斷,因?yàn)槲臋n可能看起來不清楚,我相信這里的相關(guān)點(diǎn)是以影響 equals 的方式更改",并且您似乎假設(shè)每次調(diào)用 contains 時(shí)都會(huì)調(diào)用 equals(Object).文檔沒有這么說,措辭暗示它們可能被允許緩存計(jì)算.

To extrapolate, because the docs may not appear clear, I believe the pertinent point here is `changed in a manner that affects equals', and you seem to be assuming that equals(Object) is called each time contains is invoked. The docs don't say that, the wording implies they may be allowed to cache computations.

查看source,似乎是因?yàn)槟膆ashCode 返回一個(gè)不同的值(以前是 5,現(xiàn)在是 6),可能是根據(jù)實(shí)現(xiàn)細(xì)節(jié)在不同的存儲(chǔ)桶中查找它.

Looking at the source, it seems that because your hashCode returns a different value (was 5, now 6), it's possible that it's being looked up in a different bucket based on implementation details.

這篇關(guān)于更新 Java HashMap 鍵的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉(zhuǎn)換為 Map)
Getting data from JSON(從 JSON 獲取數(shù)據(jù))
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對(duì)象列表轉(zhuǎn)換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個(gè)固定的Key對(duì)應(yīng)一個(gè)HashSet創(chuàng)建一個(gè)HashMap.出發(fā)點(diǎn))
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請(qǐng)求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 久久久亚洲一区 | 欧美激情在线一区二区三区 | 91在线网站 | 老牛嫩草一区二区三区av | jizz视频| 6996成人影院网在线播放 | 日韩精品一区二区三区四区视频 | 精品视频免费在线 | 亚洲人人| 在线播放亚洲 | 久久九九免费 | 红桃视频一区二区三区免费 | 国产成人精品一区二区三区 | 亚洲精品久久久蜜桃网站 | 国产午夜三级一区二区三 | 91免费高清 | 久久久精选 | 中文字幕精品一区 | 国产精品久久久久久久久久久久久 | 国产精品久久久久久久久免费 | 亚洲国产一区视频 | 日韩精品成人免费观看视频 | 亚洲精品亚洲人成人网 | 狠狠伊人 | 成人高清在线视频 | 国产亚洲精品美女久久久久久久久久 | 成年人黄色小视频 | 国产一区二区免费 | 在线成人免费视频 | 99精品久久久久 | 91伊人网 | 美女视频黄的免费 | 欧美一区视频 | 自拍 亚洲 欧美 老师 丝袜 | 午夜影院在线免费观看视频 | 色先锋影音 | 精品久久久久久 | 国产区视频在线观看 | 精品国产一区二区三区久久久蜜月 | 成人无遮挡毛片免费看 | 亚洲精品美女视频 |