gpt4 book ai didi

java - HashMap.get() 与继承的 hashCode() 方法一起正常工作,不能识别用户定义的 hashCode() 方法的键

转载 作者:行者123 更新时间:2023-11-30 09:15:19 24 4
gpt4 key购买 nike

我的一个类(class)中有以下方法。它只是 HashMap 的公共(public)包装器(名为 teamOfPlayer,具有 Player 对象的键和 Integer 对象的值),仅此而已。

public int getTeamOfPlayer(Player p)
{
return teamOfPlayer.get(p);
}

只要我的 Player 对象从 Object 继承了默认的 hashCode() 方法,就可以正常工作。但是,为了将我的 Player 对象保存到文件中,我实现了自己的 hashCode()。突然,该方法开始抛出 NullPointerException。

我扩展了如下方法来打印一些调试信息,但它让我比以前更加困惑。

public int getTeamOfPlayer(Player p)
{
Object[] o = teamOfPlayer.keySet().toArray();
Player p2 = (Player) o[0];

System.out.println("getTeamOfPlayer(" + p + ")"
+ "\n\thash of argument is " + p.hashCode()
+ "\n\tkeySet() of hashmap is " + teamOfPlayer.keySet()
+ "\n\tcontainsKey() of hashmap is " + teamOfPlayer.containsKey(p)
+ "\n\tplayer extracted from keySet() is " + p2
+ "\n\tplayer extracted from keySet() has hash of" + p2.hashCode()
+ "\n\targument.equals(key) returns " + p.equals(p2)
+ "\n\tkey.equals(argument) returns " + p2.equals(p));

int i = teamOfPlayer.get(p);
return i;
}

上面方法的输出在这里:

getTeamOfPlayer(main.data.entities.Player@89f632df)
hash of argument is -1980353825
keySet() of hashmap is [main.data.entities.Player@89f632df]
containsKey() of hashmap is false
player extracted from keySet() is main.data.entities.Player@89f632df
player extracted from keySet() has hash of-1980353825
argument.equals(key) returns true
key.equals(argument) returns true

“int i = teamOfPlayer.get(p);”抛出异常行,意味着 map 返回 null(因为它认为它不包含键)。我知道这就是抛出异常的原因。但是,我认为我已经证明 map 中确实存在 key 。怎么回事?

--

更新:下面是 equals() 和 hashCode() 方法。

@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;

Player player;

if (obj != null && obj instanceof Player)
player = (Player) obj;
else
return false;

if (status != player.status || !name.equals(player.name) || race != player.race || weeksOut != player.weeksOut || injuryType != player.injuryType
|| XP != player.XP)
return false;

for (int i = 0; i < 8; i++)
{
if (attributes[i] != player.attributes[i])
return false;

if (injuries[i] != player.injuries[i])
return false;
}

for (int i = 0; i < 28; i++)
{
if (hasSkill[i] != player.hasSkill[i])
return false;
}

for (int i = 0; i < 4; i++)
{
if (equipment[i] != player.equipment[i])
return false;
}

return true;
}

@Override
public int hashCode()
{
int hash = 11;

hash = 31 * hash + status;
hash = 31 * hash + name.hashCode();
hash = 31 * hash + race;
hash = 31 * hash + weeksOut;
hash = 31 * hash + injuryType;
hash = 31 * hash + XP;

for (int i = 0; i < 8; i++)
{
hash = 31 * hash + attributes[i];
hash = 31 * hash + injuries[i];
}

for (int i = 0; i < 28; i++)
hash = hash + (hasSkill[i] ? 1 : 0);

for (int i = 0; i < 4; i++)
hash = 31 * hash + equipment[i];

return hash;
}

最佳答案

这两个答案都是正确的。更具体地说

teamOfPlayer.get(p) 返回 null(可能是因为 equals() 和 hashcode() 不同步)然后 getTeamOfPlayer() 尝试将 null 转换为 int。

如果该方法返回 Integer 而不是 int,您就不会遇到这个问题,或者如果您将其编码为

public int getTeamOfPlayer(Player p)
{
Integer t = teamOfPlayer.get(p);
if (t == null) {
return -1;
}
return t;
}

你会没事的。可能你需要两者都做。一定要修复 hashCode(),而且,您可能还需要考虑不在任何球队的球员的合法情况。

关于java - HashMap.get() 与继承的 hashCode() 方法一起正常工作,不能识别用户定义的 hashCode() 方法的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19987530/

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