關於HashSet 加入重覆物件卻成功加入的問題
class A{
int a=0;
public A(int va){a=va;}
public boolean equals(Object o){
A m=(A)o;
return (a==o.a);
}
public String toString(){...return a 的String}
}
class test{
main(String[] argc)
{
HashSet<A> set= new HashSet<A>();
set.add(new A(1));
set.add(new A(2));
System.out.println(set);
}
}
結果印出來是 [1, 2] 第二次被成功加入了
可是SUN 的 Spec上明明定義set 重複的不會加入
More formally, adds the specified element, o, to this set if this set
contains no element e such that (o==null ? e==null : o.equals(e)).
可是現在看來 好像只有把address相同的才看成一樣 可是我的equals也overrinding了
也測過沒錯
我哪裡弄錯了嗎
請教板友 謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.107.75
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 9 篇):
java 近期熱門文章
PTT數位生活區 即時熱門文章