The difference analysis of Hashtable and HashMap in Java _java

Source: Internet
Author: User

1, Hashtable is a dictionary subclass,

Copy Code code as follows:

public class Hashtable<k,v>
Extends dictionary<k,v>
Implements Map<k,v>, Cloneable, java.io.Serializable

HASHMAP:
Copy Code code as follows:

public class Hashmap<k,v>
Extends abstractmap<k,v>
Implements Map<k,v>, Cloneable, Serializable

HashMap and Hashtable are all the implementation classes of the map interface;

2, the method in Hashtable is synchronous (), and the method in HashMap is not synchronized by default. In other words, in multithreaded applications, Hashtable can be safely used without specialized operations, and for HashMap, additional synchronization mechanisms are required. However, the HashMap synchronization problem can be solved by a static method of collections:

Copy Code code as follows:

public static <K,V> map<k,v> Synchronizedmap (map<k,v> m)

This method returns a synchronized map, which means that the returned map is thread safe. It is important to note that when the returned map is iterated, it must be manually synchronized on the returned map, or it will result in an indeterminate behavior:
Copy Code code 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 as synchronized block
while (I.hasnext ())
Foo (I.next ());
}

3. In HashMap, NULL can be used as a key with only one key, and a value of one or more keys can be null. When the Get () method returns a null value, you can either indicate that the key is not in HashMap or that the value of the key is null. Therefore, it is not possible in HashMap to determine whether a key exists in the HashMap by the Get () method, but rather by using the ContainsKey () method. The key value for Hashtable cannot be null, otherwise: java.lang.NullPointerException.
4.HashTable use Enumeration,hashmap to use iterator.
These are just superficial differences, and their implementations are very different.
The default size of the hash array in 5.HashTable is 11, and the increase is old*2+1. The default size of the hash array in HashMap is 16, and must be an exponent of 2.
6. The use of hash value is different, Hashtable directly use the object's Hashcode, the code is this:

Copy Code code as follows:

int hash = Key.hashcode ();
int index = (hash & 0x7fffffff)% Tab.length;

Instead, HashMap calculates the hash value and replaces it with a modulo, such as HashMap's Put method:

Copy Code code 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 Code code as follows:

static int hash (int h) {
This function ensures the hashcodes that differ
Constant multiples at each bit position have a bounded
Number of collisions (approximately 8 at default load factor).
H ^= (H >>>) ^ (h >>> 12);
Return h ^ (H >>> 7) ^ (H >>> 4);
}

Copy Code code 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.