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

HashMap 的 containsKey 方式未按預期運行

HashMap way of doing containsKey not behaving as expected(HashMap 的 containsKey 方式未按預期運行)
本文介紹了HashMap 的 containsKey 方式未按預期運行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

今天我正在做一些尋路,當我不得不使用 contains() 來查找一個類 Coord 是否在另一個 keySet() 坐標.我發現當我使用預制方法 containsKey() 時,它根本無法按我的意愿工作.我做了一個測試以了解發生了什么,結果如下:

Today I was doing some pathfinding when I had to use contains() to find if a class Coord was in an other keySet() of Coord. I found that when I used to premade method containsKey(), it was simply not working as I wanted. I made a test to find out what is happening and here it is:

HashMap<Coord, Coord> test = new HashMap<Coord, Coord>();
test.put(new Coord(3, 3), new Coord(0, 0));

System.out.println("HashMap test for containsKey : " + test.containsKey(new Coord(3, 3)));

boolean containsKey = false;
for(Coord a : test.keySet())
{
    if(a.equals(new Coord(3, 3)))
    {
        containsKey = true;
    }
}

System.out.println("My test for containsKey : "+containsKey);

令人驚訝的是,這是我發現的:

And surprisingly, here is what I found :

HashMap test for containsKey : false
My test for containsKey : true

我只是想知道發生了什么以及為什么.

I just wanted to know what is happening and why.

另外,這里是 Coord 類:

public class Coord
{
    public float x, y;

    public Coord(float a, float b)
    {
        this.x = a;
        this.y = b;
    }

    @Override
    public boolean equals(Object b)
    {
        if(b instanceof Coord)
        {
            Coord casted = (Coord) b;
            return casted.x == x && casted.y == y;
        }
        return false;
    }
}

推薦答案

HashMaps 通過哈希碼查找對象.合同的一部分是密鑰類必須覆蓋 hashCode() 以及 equals().單獨對象的默認哈希碼不相等,因此 get 找不到該對象.相反,當您遍歷所有條目時,不使用哈希碼,因此僅調用 equals 并找到對象.

HashMaps find objects by their hash code. Part of the contract is that the key class must override hashCode() as well as equals(). The default hash codes for separate objects aren't equal, so the object isn't found by get. In contrast, when you loop over all entries, the hash code isn't used, so only equals is called, and the object is found.

要使用 get 找到它,請覆蓋 Coord 中的 hashCode.

To find it using get, override hashCode in Coord.

無論何時重寫 equals,重寫 hashCode 總是好的,反之亦然.

It is always good to override hashCode whenever you override equals, and vice versa.

這篇關于HashMap 的 containsKey 方式未按預期運行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉換為 Map)
Getting data from JSON(從 JSON 獲取數據)
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個固定的Key對應一個HashSet創建一個HashMap.出發點)
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 国产农村妇女毛片精品久久麻豆 | 91精品在线播放 | 亚洲一区二区三区在线视频 | 日韩中文一区 | 色婷婷综合久久久中文字幕 | 91麻豆精品国产91久久久久久久久 | 婷婷在线免费 | 亚洲第一成人影院 | 久久久精品视 | 免费天天干 | 国产成人亚洲精品自产在线 | av免费观看在线 | 国产高清在线精品一区二区三区 | 精品国产欧美一区二区三区不卡 | 99爱在线免费观看 | 国产精品欧美精品 | 成人免费黄色片 | 精品免费国产视频 | 国产伦一区二区三区四区 | 午夜免费网站 | 久久一区二区免费视频 | 国产精品久久久久久久久久 | 精品久久久久久久久久久久 | 亚洲一区二区日韩 | 久久精品日 | 成人在线视频免费观看 | 日韩一区二区三区在线播放 | 久久久精品一区二区三区 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | 人操人人 | 成人免费在线观看 | 久久免费精品 | 四虎影院新地址 | av影音 | 婷婷激情综合 | 国产欧美日韩在线播放 | 国产激情综合五月久久 | 国产精品精品视频一区二区三区 | av三级在线观看 | 99精品视频一区二区三区 | 伊人婷婷|