HashMap 的 containsKey 方式未按预期运行

2022-10-17Java开发问题
3

本文介绍了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 方式未按预期运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13