This class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value.
fail-fast: if the Hashtable is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a {@link ConcurrentModificationException}. The Enumerations returned by Hashtables keys and elements methods are not fail-fast.
Unlike the new collection implementations, {@code Hashtable} is synchronized.
If a thread-safe implementation is not needed, it is recommended to use {@link HashMap} in place of {@code Hashtable}.
If a thread-safe highly-concurrent implementation is desired, then it is recommended to use {@link java.util.concurrent.ConcurrentHashMap} in place of {@code Hashtable}.
Dictionary
1
publicabstractclassDictionary<K,V>
The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values. Any non-null object can be used as a key and as a value. NOTE: This class is obsolete. New implementations should implement the Map interface, rather than extending this class.
An instance of Hashtable has two parameters that affect its performance: initial capacity and load factor.
The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. Note that the hash table is open: in the case of a “hash collision”, a single bucket stores multiple entries, which must be searched sequentially.
The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.
If many entries are to be made into a Hashtable, creating it with a sufficiently large capacity may allow the entries to be inserted more efficiently than letting it perform automatic rehashing as needed to grow the table.
publicsynchronized V get(Object key){ Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length;//the bucket index to store the list with specific value for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) {//find the specific one in the list return (V)e.value; } } returnnull; } 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; ("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; } privatevoidaddEntry(int hash, K key, V value, int index){ modCount++;//change structurally. Entry<?,?> tab[] = table; if (count >= threshold) { // Rehash the table if the threshold is exceeded rehash();//reconstruct the hashtable.
// Creates the new entry. ("unchecked") Entry<K,V> e = (Entry<K,V>) tab[index]; tab[index] = new Entry<>(hash, key, value, e); count++; }
rehash
Increases the capacity of and internally reorganizes this hashtable, in order to accommodate and access its entries more efficiently. This method is called automatically when the number of keys in the hashtable exceeds this hashtable’s capacity and load factor.
synchronized (this) { // Write out the threshold and loadFactor s.defaultWriteObject();
// Write out the length and count of elements s.writeInt(table.length); s.writeInt(count);
// Stack copies of the entries in the table for (int index = 0; index < table.length; index++) { Entry<?,?> entry = table[index];
while (entry != null) { entryStack = new Entry<>(0, entry.key, entry.value, entryStack); entry = entry.next; } } }
// Write out the key/value objects from the stacked entries while (entryStack != null) { s.writeObject(entryStack.key); s.writeObject(entryStack.value); entryStack = entryStack.next; } }
近期评论