Comparison of Hashtable and HashMap

Source: Internet
Author: User

Hashtable HashMap
Concurrent operations Using the synchronization mechanism, the actual application, only the synchronization of the Hashtable itself does not guarantee the correctness of the program under concurrent operation, and requires high-level concurrency protection.   The following code attempts to modify value to x+1{value = Hashtable.get (key) with the value of value equal to x corresponding to key;   if (value.intvalue () = = x) {hashtable.put (Key, New Integer (Value.intvalue () +1)); }} If 2 threads execute the above code at the same time, it may be put into a x+2 instead of x+1. There is no synchronization mechanism, requiring users to make concurrent access control
How data is traversed Iterator and enumeration Iterator
Whether to support Fast-fail With iterator traversal, support for Fast-fail with enumeration does not support Fast-fail. Support Fast-fail
Do you want to accept a key or value that is NULL? Do not accept Accept
An algorithm for calculating array subscript based on hash value When the length of the array is small, and the hash value of key is unevenly dispersed, the probability of the different hash values to get the same subscript value is higher. hash = Key.hashcode (); index= (hash&0x7fffffff)% Tab.length; Better than Hashtable, the hash of the key is done by shifting operations and bits and operations, so that it can be more widely dispersed to the different positions of the array hash = Hash (k); index = indexfor (hash, table.length);  static int hash (Object x) {int h = X.hashcode (); H + = ~ (h << 9); h ^= (H >>> 14); H + = (h << 4); H ^= (H >>> 10); return h;} static int indexfor (int h, int length) {return H & (length-1); }
The length of the entry array ø         default initial length is one, ø         can specify initial capacity when initializing Ø The default initial length is 2,Ø The length of the n-th square O-time can be specified when initializing initial capacity, if not 2, HashMap will select the first greater than I Nitial capacity's 2n square value as its initial length
Loadfactor load factor 0.75
When the load exceeds (Loadfactor * array length), the internal data is adjusted Extended array: +1 of the original array length Extended array: Original array length * 2
Both will re-place the new position in the array based on the hash value of the key. Similar algorithms, same time and space efficiency
In general, HashMap can work better and faster than Hashtable, largely thanks to its hashing algorithm, and without synchronization. Instead of relying on the synchronization of these underlying data structures, applications generally implement a protection mechanism at a higher level, so HashMap is able to meet the needs in most applications. It is recommended to use HashMap, and if synchronization is required, you can use the Sync tool class to convert it to a hashmap that supports synchronization.

Comparison of Hashtable and HashMap

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.