- static class Entry<K,V> implements Map.Entry<K,V> {
- final K key;
- V value;
- final int hash;
- Entry<K,V> next;
- /**
- * Create new entry.
- */
- Entry(int h, K k, V v, Entry<K,V> n) {
- value = v;
- next = n;
- key = k;
- hash = h;
- }
- public K getKey() {
- return HashMap.<K>unmaskNull(key);
- }
- public V getValue() {
- return value;
- }
- public V setValue(V newValue) {
- V oldValue = value;
- value = newValue;
- return oldValue;
- }
- public boolean equals(Object o) {
- if (!(o instanceof Map.Entry))
- return false;
- Map.Entry e = (Map.Entry)o;
- Object k1 = getKey();
- Object k2 = e.getKey();
- if (k1 == k2 || (k1 != null && k1.equals(k2))) {
- Object v1 = getValue();
- Object v2 = e.getValue();
- if (v1 == v2 || (v1 != null && v1.equals(v2)))
- return true;
- }
- return false;
- }
- public int hashCode() {
- return (key==NULL_KEY ? 0 : key.hashCode()) ^
- (value==null ? 0 : value.hashCode());
- }
- public String toString() {
- return getKey() + "=" + getValue();
- }
- /**
- * This method is invoked whenever the value in an entry is
- * overwritten by an invocation of put(k,v) for a key k that's already
- * in the HashMap.
- */
- void recordAccess(HashMap<K,V> m) {
- }
- /**
- * This method is invoked whenever the entry is
- * removed from the table.
- */
- void recordRemoval(HashMap<K,V> m) {
- }
- }
- Entry<K,V> e = table[bucketIndex];
- table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
以上是HashMap中摘抄的代码,今天阅读的时候一直以为第二段代码会造成循环调用,其实在new entry的时候e和table[bucketIndex]指向的就不是一个地方了,没想到一个小小的问题就让自己遇到了麻烦,看来jdk的源代码还是很有可读性的啊。
以上