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

看板java作者 (wctang)時間19年前 (2006/04/28 00:05), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串9/9 (看更多)
※ 引述《lin1000 (你是研究生嗎)》之銘言: : int hash = hash(k); : static int hash(Object x) { : int h = x.hashCode(); : h += ~(h << 9); : h ^= (h >>> 14); : h += (h << 4); : h ^= (h >>> 10); : return h; : } : static class Entry implements Map.Entry { : final Object key; : Object value; : final int hash; : Entry next; <------ Entry itself has a referece to another one : } : if (e.hash == hash && eq(k, e.key)) { <------ this line : Object oldValue = e.value; : e.value = value; : e.recordAccess(this); : return oldValue; : } : static boolean eq(Object x, Object y) { : return x == y || x.equals(y); : } 很好的示範, 這種層次的問題只要耐心 trace 一下 code, 就不需要有任何的猜測存在。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.62.106.24
文章代碼(AID): #14KElTXx (java)
討論串 (同標題文章)
文章代碼(AID): #14KElTXx (java)