- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的一个类(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/
class UserScoring implements Comparable { User user; int score; UserScoring(
当重写 Java 中的 equals() 和 hashcode() 方法时,为什么不经常使用它: public int hashCode() { return (int) this.hashC
给定java Object#hashCode文档快照: As much as is reasonably practical, the hashCode method defined by class
下面的代码(sign.hashCode())是给我签名的hashCode还是内存中对象的hash? try { PackageInfo packageInfo = getPackageMana
考虑: String[] segments = {"asdf", "qwerty", "blahblah", "alongerstring", "w349fe3434"}; String fullSt
在审查大型代码库时,我经常遇到这样的情况: @Override public int hashCode() { return someFieldValue.hashCode(); } 程序员不
在以下情况下,与下面的函数发生 HashCode 冲突的可能性有多大。 key[0]、key[1]、key[2]、key[3] 的随机整数值 使用具有以下约束的随机键值 键[0] <1,000,000
从 Java 7 开始,我们有了 o.hashCode(); Objects.hashCode(o); Objects.hash(o); 前两个与空检查大致相同,但最后一个是什么? When a si
这个问题已经有答案了: Objects.hash() vs Objects.hashCode(), clarification needed (3 个回答) 已关闭 6 年前。 一个简单、简短的问题:
我是否需要使用super.hashcode()来计算this.hashcode()? IDE(例如 IntelliJ Idea)可以生成 equals 和 hashcode。它可以使用 java.ut
class A { } class B extends A { void m1(){ System.out.println(this.hashCode());
我查看了Arrays.hashCode(char[] c)的源代码 我不太确定它适用的算法是否在所有情况下都能正常工作。 public static int hashCode(int a[])
我有两个表具有一对一的关系,如下所示: @Entity @Data @NoArgsConstructor @AllArgsConstructor public class Book { @Id
为什么stringObject的hashcode是我提供的字符串? String s = new String(); // here the hascode is 0. 但是当我获得我创建的某个对象的
public abstract class HolidayPackageVariant { private HolidayPackage holidayPackage; private String
这两个代码片段有什么区别? 片段 1: Object o = new Object(); int i = Objects.hashCode(o); 片段 2: Object o = new Objec
在 Java 8 中有一个类 java.util.Objects,其中包含 hashCode() 方法。同时 Google Guava 19 包含 com.google.common.base.Obj
我的一个类(class)中有以下方法。它只是 HashMap 的公共(public)包装器(名为 teamOfPlayer,具有 Player 对象的键和 Integer 对象的值),仅此而已。 pu
我在这里做错了什么? @Override public int hashCode() { HashCodeBuilder has
我有以下程序。 Employee employee1 = new Employee("Raghav1", 101); Employee employee2 = new Employee("Raghav
我是一名优秀的程序员,十分优秀!