關於HashSet 加入重覆物件卻成功加入的問題

看板java作者時間19年前 (2006/04/27 16:44), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/9 (看更多)
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
文章代碼(AID): #14K8IALl (java)
討論串 (同標題文章)
文章代碼(AID): #14K8IALl (java)