What is the difference between hashtable and hashmap?

Source: Internet
Author: User

A. hashtable inherits from the obsolete dictionary class. hashmap inherits from the abstractmap class and is also an implementation of the map interface introduced by Java 1.2. B. Perhaps the most important difference is that the hashtable method is synchronous, while the hashmap method is not. This means that, although you can use a hashtable in a multi-threaded application without taking any special actions, you must also provide external synchronization for a hashmap. A convenient method is to use the static synchronizedmap () method of the collections class to create a thread-safe map object and return it as an encapsulated object. The method of this object allows you to access the potential hashmap synchronously. The result is that when you do not need synchronization, you cannot cut off the synchronization in hashtable (for example, in a single-threaded application), and synchronization increases a lot of processing costs. C. The third difference is that only hashmap allows you to use a null value as the key or value of a table entry. Only one record in hashmap can be an empty key, but any number of entries can be empty values. That is to say, if no search key is found in the table, or if a search key is found but it is an empty value, get () returns NULL. If necessary, use the containkey () method to differentiate the two cases. D. hashmap removes the hashtable contains method and retains the default size of the hash array in the containsvaluecontainskey Method E. hashtable, which is increased by the old * 2 + 1 method. The default size of the hash array in hashmap is 16, and it must be an index of 2.

 

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.