检查和删除Java HashMap中的元素 [英] Checking for and Removing elements in Java HashMap

查看:150
本文介绍了检查和删除Java HashMap中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java中的HashMap检查和删除元素。它的键是我创建的一个名为ClusterKey的类型,它的值是我创建的一个名为ClusterValue的类型。

I am trying to check for and remove elements, using a HashMap in Java. Its keys are a type I created called ClusterKey, and its values are a type I created called ClusterValue.

这是导致问题的代码:

ClusterKey ck = new ClusterKey(Long.parseLong(split[0].split("=")[1]), 
                        Integer.parseInt(split[1].split("ey")[1]));
if (arg == 0) keys.put(ck, new ClusterValue(index, false));
if (arg == 1) {
    if (keys.containsKey(ck)) {
        index = keys.get(ck).messageNo;
        keys.remove(ck);
    }
    keys.put(ck, new ClusterValue(index, true));
}

问题是,即使ClusterKey与现有的ClusterKey相同, containsKey()和remove()似乎并不认为它是平等的。我已经在类ClusterKey中实现了equals()来覆盖Java的equals()方法,如下所示:

The problem is that even when the ClusterKey is the same as an existing ClusterKey, containsKey() and remove() do not seem to recognize it as equal. I have implemented equals() in class ClusterKey to override Java's equals() method, as follows:

class ClusterKey {
    long firstKey;
    int secondKey;
    public ClusterKey(long firstKey, int secondKey) {
        this.firstKey = firstKey;
        this.secondKey = secondKey;
    } public boolean equals(Object otherKey) {
        return this.firstKey == ((ClusterKey) otherKey).firstKey && this.secondKey == ((ClusterKey) otherKey).secondKey;
    }
}

所以,我很困惑。感谢您的帮助。

So, I'm quite confused. Thanks so much for your help.

问候,
Rebecca

Regards, Rebecca

更新:感谢您的支持对我的代码的建议和反馈。我可以通过向ClusterKey添加hashCode()来解决这个问题,如下所示:

UPDATE: Thank you for your advice and feedback on my code. I was able to solve the problem by adding hashCode() to ClusterKey, as follows:

    } public boolean equals(Object otherKey) {
        return this.firstKey == ((ClusterKey) otherKey).firstKey && this.secondKey == ((ClusterKey) otherKey).secondKey;
    } public int hashCode() {
        return (int) firstKey + secondKey;
    } 


推荐答案

(如 HashMap HashSet )正常工作,其元素必须覆盖 hashCode() code>除了 equals()方法。原因是哈希代码用于标识在查找过程中放置​​元素(在插入过程中)或搜索(在查找期间使用 equals())的存储桶。

For any Hash enabled data structure (like HashMap, HashSet) to work correctly its elements must override hashCode() in addition to the equals() method. The reason being that the hash code is used to identify the bucket in which to put the element (during insertion) or search in (using equals() during a lookup).

如果您不重写 hashCode(),则从 Object#hashCode )被使用,即使对于您认为等效的对象也会返回不同的值( equals()方法返回 true for)。

If you do not override hashCode(), the default implementation from Object#hashCode() is used which would return different values even for the objects that you consider equivalent (the equals() method returns true for).

这就是为什么你的

This is why your

 hashMap.containsKey(ClusterKey key)

调用失败,尽管密钥已经存在。因为,哈希码不匹配 HashMap 决不会在右边的桶中查找关键字。因此,您的 equals()永远不会在这里被调用。

calls are failing in spite of the key already being present. Since, the hash codes don't match the HashMap never looks for the key in the right bucket. Hence, your equals() never gets called here.

这篇关于检查和删除Java HashMap中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆