gpt4 book ai didi

具有覆盖的 hashCode() 和 equals() 的 Java HashMap 不返回任何数据

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:41:45 25 4
gpt4 key购买 nike

我最近一直在使用 Java 的 HashMap,并且遇到了一些有趣的行为。我目前正在使用它来存储具有多个字段的键/值对象。为此,我重写了 hashCode() 和 equals(),如下所示:

public final class TransitionState {

private String mStackSymbol;
private String mTransitionSymbol;
private int mState;

private static final int HASH_SEED = 7; //Should be prime
private static final int HASH_OFFSET = 31;

//Constructor and getter methods here

public boolean equals(TransitionState other) {

//Check that we aren't comparing against ourself
if (this == other) {
return true;
}

//Check that we are not comparing against null
if (other == null) {
return false;
}

//Check fields match
if ((mState == other.getState()) &&
(mTransitionSymbol.equals(other.getTransitionSymbol())) &&
(mStackSymbol.equals(other.getStackSymbol()))) {

return true;

} else {
return false;

}
}

public int hashCode() {
int intHash = HASH_SEED;

//Sum hash codes for individual fields for final hash code
intHash = (intHash * HASH_OFFSET) + mState;
intHash = (intHash * HASH_OFFSET) + (mTransitionSymbol == null ? 0 : mTransitionSymbol.hashCode());
intHash = (intHash * HASH_OFFSET) + (mStackSymbol == null ? 0 : mStackSymbol.hashCode());

return intHash;
}
}

现在,我可以毫无问题地将项目放入 map 中。然而,取回它们是另一回事。每当我尝试从 HashMap 获取 get() 时,都会返回 NULL。我编写了一些测试代码来遍历 Map 和打印值,这是让事情变得困惑的地方,因为我的关键对象的 hashCode() 与我在 map 中的匹配并且与已知值相等返回 true。示例输出如下(见表底部的第四个转换):

Transition Table:
State Symbol Stack Move
--------------------------
1, a, b, (1, pop) with key hashcode 212603 and value hashcode 117943
0, b, a, (0, pop) with key hashcode 211672 and value hashcode 117912
1, b, z, (1, push) with key hashcode 212658 and value hashcode 3459456
0, a, b, (0, pop) with key hashcode 211642 and value hashcode 117912
1, a, z, (0, push) with key hashcode 212627 and value hashcode 3459425
0, a, a, (0, push) with key hashcode 211641 and value hashcode 3459425
0, a, z, (0, push) with key hashcode 211666 and value hashcode 3459425
0, b, z, (1, push) with key hashcode 211697 and value hashcode 3459456
1, b, a, (1, pop) with key hashcode 212633 and value hashcode 117943
1, b, b, (1, push) with key hashcode 212634 and value hashcode 3459456

ababba

Transition from (0, a, z) with hashcode 211666
transition.equals(new TransitionState(0, "a", "z")) = true
HashMap containsKey() = false
Transition not found
false

如您所见, key 将哈希码与映射中的条目相匹配,但我被告知该条目不存在。我尝试调试 HashMap 的 containsKey() 方法,该方法执行 get() 检查 NULL。进入 get() 显示循环在返回 NULL 之前只运行一次。

那么,这是一个 HashMap 问题(可能不是)还是(更有可能)我做错了什么?预先感谢您的帮助。

最佳答案

你还没有覆盖 equals 正确...你需要

 public boolean equals(Object other)

目前您只是重载它。

基本上,真正的覆盖仍然可以完成与现有覆盖相同的工作,但首先要进行测试:

if (!(other instanceof TransitionState))
{
return false;
}
TransitionState otherState = (TransitionState) other;
// Now do the rest of the comparison

请注意,在这种情况下您不需要检查 null,因为它会导致 instanceof 测试失败。

现在 Java 允许您添加注释来告诉编译器您真的要覆盖父方法:

 @Overrides
public boolean equals(Object other)

现在,如果您在名称中输入错误或签名错误,编译器会告诉您。

关于具有覆盖的 hashCode() 和 equals() 的 Java HashMap 不返回任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4269839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com