作者qrtt1 (null)
站内java
标题Re: [问题] 请问如何实作两个key和一个value的对应 …
时间Thu May 15 22:35:14 2008
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
1F:推 Nt1:谢谢,我会试试看的! 05/16 01:43