作者superlubu (叔叔你人真好)
看板java
标题Re: [问题] HashMap: 使用自定类别当作key
时间Fri May 22 16:50:09 2009
※ 引述《hstt ()》之铭言:
: → hstt:请问是指string要用equals来做吗?不过为啥放在SET时却正常@@ 05/22 15:08
不知你是怎样做 test 的,不过我试过这样:
public static void main(String[] argv) {
HashMap<Pair, Integer> hm = new HashMap<Pair, Integer>();
Pair p1 = new Pair(new String("s"), new String("p")),
p2 = new Pair(new String("p"), new String("s"));
hm.put(p1, 1);
System.out.println(hm.keySet().contains(p2));
Set<Pair> s = new HashSet<Pair>();
s.add(p1);
System.out.println(s.contains(p2));
}
若使用 == 来 compare, 两个 output 都是 false 的...
使用 equals 来 compare, 则是 true.
--
《为了要得到真相,就要向原 PO 伸图》
那就是伸图魔人的没图没真相原则,那时我们坚信那就是逼逼死的真实
靠么,图咧?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 147.8.127.102