HashMap,set??

1792 ワード

 /*
 hashMap         
        hashCode           
*/

transient Entry[] table;

/*
         hashCode    ,         table[hashcode] 
     ,  Entry     next    
*/

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;
        }


/*
  i   hash  
*/
public V put(K key, V value) {
	K k = maskNull(key);
        int hash = hash(k);
        int i = indexFor(hash, table.length);

        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
////hash  &&equals is true        (key)
            if (e.hash == hash && eq(k, e.key)) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        addEntry(hash, k, value, i);
        return null;
    }

//eq method
 static boolean eq(Object x, Object y) {
        return x == y || x.equals(y);
    }

 
 
 
  //HashSet


private transient HashMap<E,Object> map;

    // Dummy value to associate with an Object in the backing Map
    private static final Object PRESENT = new Object();