"Java" Hashtable and HashMap differences

Source: Internet
Author: User

① Inherit different
public class Hashtable extends Dictionary implements maps public class HashMap extends Abstractmap implements map
The methods in ②hashtable are synchronous, and the methods in HashMap are not synchronized by default.

In the context of multi-threaded concurrency, you can use Hashtable directly, but to use hashmap, you need to increase the synchronization process.

In ③hashtable, both key and value do not allow null values.

In HashMap, NULL can be used as a key with only one key, and one or more keys can have a value of NULL. When the Get () method returns a null value, it can indicate that the key is not in the HashMap, or that the value corresponding to the key is null. Therefore, the get () method cannot be used in HashMap to determine whether a key exists in HashMap and should be judged by the ContainsKey () method.

④ the internal implementation of the two traversal methods is different.

Hashtable and HashMap all use the Iterator. For historical reasons, Hashtable also used the enumeration approach.

⑤ hash values are used differently.

Hashtable directly uses the object's hashcode. The hash value is recalculated by the HashMap.

⑥hashtable and hashmap their two internal implementations of the array's initial size and the way the capacity is expanded.

The default size of the hash array in Hashtable is 11, and the increment is old*2+1. The default size of the hash array in HashMap is 16, and must be a 2 index.

"Java" Hashtable and HashMap differences

Related Article

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.