Http://blog.csdn.net/java2000_net/archive/2008/06/05/2512510.aspx
Let's first look at the definition of 2 classes.
[Java]View Plain Copy
- public class Hashtable
- Extends Dictionary
- Implements Map, Cloneable, java.io.Serializable
[Java]View Plain Copy
- public class HashMap
- Extends Abstractmap
- Implements Map, Cloneable, Serializable
Visible hashtable inherit from Dictiionary and HashMap inherit from Abstractmap
The Put method of Hashtable is as follows
[Java]View Plain Copy
- Public synchronized v put (K key, V value) {//###### Note here 1
- Make sure the value was not null
- if (value = = null) {//###### NOTE here 2
- throw new NullPointerException ();
- }
- Makes sure the key is not already in the Hashtable.
- Entry tab[] = table;
- int hash = Key.hashcode (); ###### Note here 3
- int index = (hash & 0x7FFFFFFF)% Tab.length;
- for (Entry e = Tab[index]; E! = null; e = e.next) {
- if ((E.hash = = hash) && e.key.equals (key)) {
- V old = E.value;
- E.value = value;
- return old;
- }
- }
- modcount++;
- if (count >= threshold) {
- Rehash the table if the threshold is exceeded
- Rehash ();
- tab = table;
- Index = (hash & 0x7FFFFFFF)% Tab.length;
- }
- Creates the new entry.
- Entry e = Tab[index];
- Tab[index] = new Entry (hash, key, value, E);
- count++;
- return null;
- }
Note The 1 method is synchronous
Note the 2 method does not allow Value==null
Note that the 3 method calls the Hashcode method of key, and if key==null, throws a null pointer exception HashMap The Put method is as follows
[Java]View Plain Copy
- Public V put (K key, V value) {//###### Note here 1
- if (key = = null)//###### Note here 2
- return Putfornullkey (value);
- int hash = hash (Key.hashcode ());
- int i = indexfor (hash, table.length);
- for (Entry e = table[i]; E! = null; e = e.next) {
- Object K;
- if (E.hash = = Hash && (k = e.key) = = Key | | key.equals (k)) {
- V oldValue = E.value;
- E.value = value;
- E.recordaccess (this);
- return oldValue;
- }
- }
- modcount++;
- AddEntry (hash, key, value, I); ######, watch this.
- return null;
- }
Note The 1 method is non-synchronous
Note The 2 method allows key==null
Note the 3 method does not make any calls to value, so allow null
Add:
Hashtable has a contains method, easy to cause misunderstanding, so in hashmap inside has been removed
Of course, all 2 classes use the ContainsKey and Containsvalue methods.
HashMap Hashtable
Parent class Abstractmap Dictiionary
Whether to synchronize No is
K,v can null whether
HashMap is a lightweight implementation of Hashtable (non-thread-safe implementation), they all complete the map interface,
The main difference is that HASHMAP allows null (NULL) key values (key), which may be more efficient than Hashtable due to non-thread safety.
HashMap allows NULL to be used as a entry key or value, and Hashtable is not allowed.
HashMap hashtable contains method removed, changed to Containsvalue and ContainsKey. Because the contains method is easy to cause misunderstanding.
Hashtable inherits from the dictionary class, and HashMap is an implementation of the map interface introduced by Java1.2.
The biggest difference is that the Hashtable method is synchronize, and HashMap is not, when multiple threads access Hashtable, they do not need to synchronize their methods, and HashMap must provide it with external synchronization ( COLLECTIONS.SYNCHRONIZEDMAP).
The Hash/rehash algorithms used by Hashtable and HashMap are probably the same, so there is no big difference in performance.
HashMap Hashtable Difference