Re: [問題] 請問如何實作兩個key和一個value的對應 …
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
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):