What is the difference between HashMap and Hashtable? hashmaphashtable

Source: Internet
Author: User

What is the difference between HashMap and Hashtable? hashmaphashtable
Differences between HashMap and Hashtable

1. Different inherited parent classes:

The parent class of HashTable is Dictionary.

The parent class of HashMap is AbstractMap;

2. thread security:

HashTable: thread security. The HashTable method is modified by synchronized.

HashMap: thread security

3. contains ()

HashTable: Reserved contains (), containsKey (), containsValue ()

HashMap: contains () is removed, and containsKey () and containsValue () are retained ()

4. Whether the key and value can be null

HashTable: key and value cannot be empty. the reason is that the method in the source code will traverse the entry and call equals () with the key or value of the entry. Therefore, you must first determine whether the key/value is null. If it is null, an exception will be thrown.

HashMap: key, value can be null. null can be used as only one key, and null can be used as value to have multiple

5. Different Internal implementations of traversal methods

HashTable: Use Enumeration, Iterator

HashMap: Use Iterator

6. the array initialization and expansion modes used by the internal implementation are different.

HashTable: the initial size of the array is 11, and the expansion mode is 2 * old + 1

HashMap: the exponential power of the array with the initial size of 16 and the expansion method of 2

7. Different hash values

HashTable: directly use hashcode ()

HashMap: Re-calculate the hash value

 

Only list the differences. The detailed differences will be added slowly...

 

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.