Differences between Hashtable and HashMap in java

Source: Internet
Author: User

1. Hashtable is a subclass of Dictionary,

Copy codeThe Code is as follows: public class Hashtable <K, V>
Extends Dictionary <K, V>
Implements Map <K, V>, Cloneable, java. io. Serializable

HashMap:Copy codeThe Code is as follows: public class HashMap <K, V>
Extends AbstractMap <K, V>
Implements Map <K, V>, Cloneable, Serializable

HashMap and Hashtable are both implementation classes of the Map interface;

2. The methods in Hashtable are synchronized (), while those in HashMap are not synchronized by default. That is to say, Hashtable can be safely used in multi-threaded applications without special operations. For HashMap, additional synchronization mechanisms are required. However, the synchronization problem of HashMap can be solved through a static method of Collections:

Copy codeThe Code is as follows: public static <K, V> Map <K, V> synchronizedMap (Map <K, V> m)

This method returns a synchronized Map, that is, the returned Map is thread-safe. Note that when performing iteration on the returned map, you must manually synchronize the returned map. Otherwise, the returned map may cause uncertain behavior:Copy codeThe Code is as follows: Map m = Collections. synchronizedMap (new HashMap ());
...
Set s = m. keySet (); // Needn't be in synchronized block
...
Synchronized (m) {// Synchronizing on m, not s!
Iterator I = s. iterator (); // Must be in synchronized block
While (I. hasNext ())
Foo (I. next ());
}

3. In HashMap, null can be used as a key, and there is only one such key. One or more keys can correspond to null values. When the get () method returns a null value, it can indicate that the key does not exist in HashMap or that the corresponding value of the key is null. Therefore, the get () method cannot be used to determine whether a key exists in the HashMap. Instead, the containsKey () method should be used to determine whether a key exists in the HashMap. The key value of Hashtable cannot be null. Otherwise: java. lang. NullPointerException.
4. HashTable uses Enumeration and HashMap uses Iterator.
The above is just a difference on the surface, and their implementation is also very different.
5. The default size of the hash array in HashTable is 11, and the increase is in the old * 2 + 1 mode. The default size of the hash array in HashMap is 16, and it must be an index of 2.
6. For different hash values, HashTable directly uses the hashCode of the object. The Code is as follows:

Copy codeThe Code is as follows: int hash = key. hashCode ();
Int index = (hash & 0x7FFFFFFF) % tab. length;

HashMap recalculates the hash value, and replaces the modulo with and. For example, the put Method of HashMap:

Copy codeThe Code is as follows: public V put (K key, V value ){
If (key = null)
Return putForNullKey (value );
Int hash = hash (key. hashCode ());
Int I = indexFor (hash, table. length );
For (Entry <K, V> 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 );
Return null;
}

Copy codeThe Code is as follows: static int hash (int h ){
// This function ensures that hashCodes that differ only
// Constant multiples at each bit position have a bounded
// Number of collisions (approximately 8 at default load factor ).
H ^ = (h >>> 20) ^ (h >>>> 12 );
Return h ^ (h >>> 7) ^ (h >>> 4 );
}

Copy codeThe Code is as follows: static int indexFor (int h, int length ){
Return h & (length-1 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.