为什么自定义对象不等于 HashMap 的键?

2023-11-27

我在使用自己的类作为 HashMap 的键时遇到问题

 public class ActorId {
     private final int playerId;
     private final int id;

     ActorId(int playerId, int id) {
         this.playerId = playerId;
         this.id = id;
     }

     public boolean equals(ActorId other) {
         return this.id == other.id && this.playerId == other.playerId;
     }

     public int hashCode() {
         int hash = 1;
         hash = hash * 31 + playerId;
         hash = hash * 31 + id;
         return hash;
     }

     public String toString() {
         return "#" + playerId + "." + id;
     }

     public int getPlayerId() {
         return playerId;
     }
 }

这是一个失败的 JUnit 测试

 import static org.junit.Assert.*;
 import java.util.Map;
 import org.junit.Test;

 public class ActorIdTest {
     @Test
     public final void testAsMapKey() {
         ActorId a = new ActorId(123, 345);
         ActorId b = new ActorId(123, 345);

         assertTrue(a.equals(b));
         assertEquals(a.hashCode(), b.hashCode());

         // Works with strings as keys
         Map<String, String> map1 = new java.util.HashMap<String, String>();

         map1.put(a.toString(), "test");
         assertEquals("test", map1.get(a.toString()));
         assertEquals("test", map1.get(b.toString()));
         assertEquals(1, map1.size()); 

         // But not with ActorIds
         Map<ActorId, String> map2 = new java.util.HashMap<ActorId, String>();

         map2.put(a, "test");
         assertEquals("test", map2.get(a));
         assertEquals("test", map2.get(b)); // FAILS here
         assertEquals(1, map2.size()); 

         map2.put(b, "test2");
         assertEquals(1, map2.size());
         assertEquals("test2", map2.get(a));
         assertEquals("test2", map2.get(b));
     }
 }

你需要改变

public boolean equals(ActorId other) {
    ....
}

to

public boolean equals(Object other) {
    ....
}

每日提示:总是使用@Override注解。

如果您使用过@Override注释,编译器会捕获错误并说:

ActorId 类型的方法 equals(ActorId) 必须重写或实现超类型方法

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么自定义对象不等于 HashMap 的键? 的相关文章

随机推荐

  • PHP 命名空间和 require

    我需要在主 index php 文件中包含几个文件 我正在使用命名空间 我可以使用 include require 并使文件使用与 index php 相同的命名空间 而不在每个包含的文件中指定命名空间和 use 语句吗 PHP 命名空间范
  • 在 TensorFlow 中对数组进行排序

    假设我在 TensorFlow 中有一个数组 0 12300211 0 51767069 0 13886075 0 55363625 0 47279349 0 50432992 0 48080254 0 51576483 0 8434793
  • 使用 Jasmine 监视没有对象的函数

    我正在使用 Jasmine 并且有一个库 js 文件 其中包含许多不与任何对象关联的函数 即全局函数 我该如何监视这些功能 我尝试使用窗口 文档作为对象 但即使调用该函数 间谍也无法工作 我还尝试将其包装在一个假对象中 如下所示 var f
  • 如何避免单击按钮时的 Page_Load() ?

    我有两个按钮 预览和保存 借助预览按钮 用户可以根据格式查看数据 然后保存 但是 当单击预览时 附加到 ajaxcontrol 日历 的一个文本框将变为空 用户必须在保存之前填写日期 这要怎么处理呢 在预览时单击我获取详细信息以在布局中显示
  • Python 中的按键

    Is it possible to make it appear to a system that a key was pressed for example I need to make A key be pressed thousand
  • 使用 hadoop 运行 jar 时 NoSuchMethodError Sets.newConcurrentHashSet()

    我在用着cassandra all 2 0 7api 与Hadoop 2 2 0
  • 使用zef安装模块后如何编辑模块中的源代码?

    例如 我已经安装了Cro模块 当我运行我的简单代码时 my headers Authorization gt OAuth realm oauth consumer key xxxxxxxxxxxxxxxx oauth nonce 29515
  • 将数组写入 csv python(一列)

    我正在尝试将数组的值写入 python 中的 csv 文件 但是当我在Excel中打开文件时 数据显示在一行中 我想要一列 其中数组的每个成员都是一行 数组 testLabels 的形式为 array deer airplane dog f
  • 如何在 Jekyll 标签插件中获取 Markdown 处理过的内容

    我正在为我的 Octopress 网站开发 Jekyll 标签插件 以帮助我制作 注释 元素 我只是希望能够在我的博客上突出显示一条信息作为旁注 就像这样 问题是 我不知道如何处理此标签的内容 即 Markdown 或 Textile 上面
  • c 中的外部和全局

    谁能告诉我使用有什么特殊要求吗EXTERN or GLOBALC 程序中的变量 如果我从 gloabl 更改为 extern 我看不出像下面这样的程序有任何区别 include
  • 获取流式hadoop程序中的输入文件名

    在用 Java 编写程序时 我可以使用 FileSplit 找到映射器类中的输入文件的名称 当我用Python编写程序 使用流式传输 时 是否有相应的方法可以做到这一点 我在apache上的hadoop流媒体文档中发现了以下内容 请参阅配置
  • 使用 python 3.3 生成并保存 .eml 文件

    我正在尝试使用标准电子邮件库生成电子邮件并将其保存为 eml 文件 我一定不明白 email generator 是如何工作的 因为我不断收到错误 AttributeError str 对象没有属性 write from email imp
  • Python 中的多维/多变量动态时间扭曲 (DTW) 库/代码

    我正在研究时间序列数据 可用的数据是多变量的 因此 对于每个时间实例 都有三个可用的数据点 格式 X 是 Z 这样就会实时生成一个上述格式的时间序列数据 我试图在另一个已经存储的时间序列基础数据中找到这个实时生成的时间序列的良好匹配 其大小
  • 如何在循环数组时向数组添加项目? [复制]

    这个问题在这里已经有答案了 On a foreach循环 似乎 PHP 在开始时读取整个数组 因此如果您突然需要将新项目追加到数组中 循环将不会处理它们 a array 1 2 3 4 5 6 7 8 9 10 foreach a as b
  • 不希望原始 data.table 在传递给函数时被修改

    我是以下的粉丝data table 为满足当前和未来的所有需求编写可重用的函数 这是我在解决这个问题时遇到的挑战 使用 ggplot2 自动绘制所有 data table 列的最佳方法 我们将 data table 传递给函数进行绘图 然后
  • MongoDB分片,添加新节点时如何重新平衡?

    我正在尝试了解 MongoDB 和分片的概念 如果我们从 2 个节点开始 并根据姓氏对客户数据进行分区 其中 A 到 M 数据存储在节点 1 上 N 到 Z 数据存储在节点 2 上 当我们想要横向扩展并添加更多节点时会发生什么 我只是不明白
  • 为TaskTracker子进程配置内存的不同方式(Mapper和Reduce任务)

    设置和设置有什么区别mapred job map 内存 mb and mapred child java opts使用 Xmx 来控制Mapper 和Reduce 任务使用的最大内存 哪一个优先 Xmx指定分配的jvm的最大堆空间 这是为对
  • 用Java生成真值表

    我正在尝试打印一些真值表作为学校作业的一部分 如何在 Java 中生成动态大小真值表 So that printTruthTable 1 prints 0 1 printTruthTable 3 prints 0 0 0 0 0 1 0 1
  • 在 Hyperledger Fabric CA 中注册和注册证书有什么区别

    在 Hyperledger Fabric CA 中注册和注册证书之间到底有什么区别 我是密码学新手 我对 Fabric CA 的工作感到非常困惑 此外 通过 cryptogen 生成的证书与通过 Fabric CA 生成的证书不同 因此 根
  • 为什么自定义对象不等于 HashMap 的键?

    我在使用自己的类作为 HashMap 的键时遇到问题 public class ActorId private final int playerId private final int id ActorId int playerId int