久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么自定義對象不是 HashMap 的等效鍵?

Why are custom objects not equivalent keys for a HashMap?(為什么自定義對象不是 HashMap 的等效鍵?)
本文介紹了為什么自定義對象不是 HashMap 的等效鍵?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在使用自己的類作為 HashMap 的鍵時遇到問題

I'm having trouble using my own class as a key for a 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 測試

Here is a failing JUnit test

 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) {
    ....
}

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

<小時>

每日提示:始終使用 @Override 注釋.

如果你使用了 @Override 注釋,編譯器會捕捉到錯誤并說:

If you had used the @Override annotation, the compiler would have caught the error and said:

ActorId 類型的方法 equals(ActorId) 必須重寫或實現超類型方法

The method equals(ActorId) of type ActorId must override or implement a supertype method

這篇關于為什么自定義對象不是 HashMap 的等效鍵?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉換為 Map)
Getting data from JSON(從 JSON 獲取數據)
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個固定的Key對應一個HashSet創建一個HashMap.出發點)
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 羞羞视频在线观看 | 久久久久久免费毛片精品 | 黄色一级特级片 | 国产一区欧美 | caoporn视频在线 | 毛片免费视频 | 精品欧美乱码久久久久久1区2区 | 国产成人99久久亚洲综合精品 | 久久久久亚洲视频 | 国产91丝袜在线播放 | 国产高清视频 | 日韩一区二区免费视频 | 亚洲精品黑人 | 亚洲精品一区国语对白 | 日日夜夜天天 | 欧美视频在线一区 | 亚洲欧美高清 | 国产一区二区三区免费观看视频 | av综合站| 黄片毛片免费观看 | 成人性生交大片免费看中文带字幕 | 国产一区二区在线免费 | 毛片国产 | 亚洲免费在线播放 | 久久久999成人 | 国产精品电影网 | 91精品国产日韩91久久久久久 | 欧美日韩在线一区 | 国产色网站| 91成人免费看片 | 日韩免费福利视频 | 96国产精品久久久久aⅴ四区 | 激情五月综合 | 久久亚洲一区二区三区四区 | 精品国产乱码久久久久久果冻传媒 | 久久久久久久久淑女av国产精品 | 日本理论片好看理论片 | 日韩免费一区二区 | 日韩高清中文字幕 | 黄色在线免费观看视频 | 免费观看黄色片视频 |