■HashMap#get()メソッドの解析 transient Entry[] table; static final Object NULL_KEY = new Object(); public Object get(Object key) { Object k = maskNull(key); int hash = hash(k); int i = indexFor(hash, table.length); Entry e = table[i]; while (true) { if (e == null) return e; if (e.hash == hash && eq(k, e.key)) return e.value; e = e.next; } } static Object maskNull(Object key) { return (key == null ? NULL_KEY : key); } static int hash(Object x) { int h = x.hashCode(); h += ~(h << 9); h ^= (h >>> 14); h += (h << 4); h ^= (h >>> 10); return h; } get()メソッドの2行目 int hash = hash(k)でhash()メソッドを呼ぶ。 hash()メソッドは、引数のオブジェクトObject xつまり、get(Object key)で取得したオブジェトを 元にハッシュコードを生成している。以下がサンプル Map m = new HashMap(); map.put("hoge","fuga"); map.get("hoge"); // StringのhashCode()メソッド ■put()メソッドの解析 public Object put(Object key, Object value) { Object k = maskNull(key); int hash = hash(k); int i = indexFor(hash, table.length); for (Entry e = table[i]; e != null; e = e.next) { if (e.hash == hash && eq(k, e.key)) { Object oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(hash, k, value, i); return null; } Entryクラスがどこにあるのか、探し回ったところ Entryインタフェースは、Mapクラスの内部(インナインタフェース)で定義されている。 for文の中を確認してみるともし、hashが同じならエントリに含めない。 その証明として、HashMap#recordAccess()は、以下のようにダミーである。 これは、Overrideする設計だろう。 void recordAccess(HashMap m) { }