publicclassHashtable<K,V> extendsDictionary<K,V> implementsMap<K,V>, Cloneable, java.io.Serializable{ /** * Constructs a new, empty hashtable with a default initial capacity (11) * and load factor (0.75). */ publicHashtable(){ this(11, 0.75f); } publicsynchronized V put(K key, V value){ // Make sure the value is not null if (value == null) { thrownew NullPointerException(); }
// Makes sure the key is not already in the hashtable. Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; @SuppressWarnings("unchecked") Entry<K,V> entry = (Entry<K,V>)tab[index]; for(; entry != null ; entry = entry.next) { if ((entry.hash == hash) && entry.key.equals(key)) { V old = entry.value; entry.value = value; return old; } }
addEntry(hash, key, value, index); returnnull; } publicsynchronized V remove(Object key){ Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; @SuppressWarnings("unchecked") Entry<K,V> e = (Entry<K,V>)tab[index]; for(Entry<K,V> prev = null ; e != null ; prev = e, e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { modCount++; if (prev != null) { prev.next = e.next; } else { tab[index] = e.next; } count--; V oldValue = e.value; e.value = null; return oldValue; } } returnnull; } publicsynchronizedbooleanisEmpty(){ return count == 0; } publicsynchronizedintsize(){ return count; } publicsynchronizedbooleancontainsKey(Object key){ Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { returntrue; } } returnfalse; } publicbooleancontainsValue(Object value){ return contains(value); } publicsynchronizedbooleancontains(Object value){ if (value == null) { thrownew NullPointerException(); }
Entry<?,?> tab[] = table; for (int i = tab.length ; i-- > 0 ;) { for (Entry<?,?> e = tab[i] ; e != null ; e = e.next) { if (e.value.equals(value)) { returntrue; } } } returnfalse; } }