Re: 關於HashSet 加入重覆物件卻成功加入的問題
※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 9 之 9 篇):
java 近期熱門文章
PTT數位生活區 即時熱門文章