Re: [問題] 請問如何實作兩個key和一個value的對應 …

看板java作者 (null)時間17年前 (2008/05/15 22:35), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
Earvin 提出了疑問, 我自己也試了 equals/hashCode 的實作有問題 所以我改了一下 import java.util.HashSet; public class Pair<M, N> extends HashSet{ public Pair(M m, N n) { this.add(m); this.add(n); } @Override public boolean equals(final Object other) { if (!(other instanceof Pair)) return false; return super.equals(other); } @Override public int hashCode() { return super.hashCode(); } public static void main(String[] args) { boolean b = new Pair<Integer, Integer>(1234567, 7654321) .equals(new Pair<Integer, Integer>(7654321, 1234567)); System.out.println(b); // true // true System.out.println( new Pair("a", "b").equals(new Pair("b", "a"))); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.133.80.216

05/16 01:43, , 1F
謝謝,我會試試看的!
05/16 01:43, 1F
文章代碼(AID): #18B4eZ4z (java)
文章代碼(AID): #18B4eZ4z (java)