A brief analysis of the difference between map and Hashmap,hashtable,hashset in Java _java

Source: Internet
Author: User

The difference between Hashtable and HashMap

First, the inherited parent classes are different.
Hashtable inherits from the dictionary class, and HashMap inherits from the Abstractmap class. But both implement the map interface.

Copy Code code as follows:

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

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


second, thread security is different.
The methods in Hashtable are synchronize, and the methods in HashMap are synchronize by default. In the context of multithreading concurrency, you can directly use Hashtable, do not need to implement their own methods of synchronization, but the use of hashmap must increase their own synchronization process.

Third, whether to provide contains method
HashMap hashtable contains method removed, changed to Containsvalue and ContainsKey, because the contains method is easy to cause misunderstanding.

Hashtable retains the Contains,containsvalue and ContainsKey three methods, where the contains and Containsvalue functions are the same.

Four, key and value allow null values.
Where key and value are objects and cannot contain duplicate keys, they can contain duplicate value.
In Hashtable, null values are not allowed for key and value.
In HashMap, NULL can be a key, with only one key, and a value of one or more keys to null. When the Get () method returns a null value, it may be that the key is not in the hashmap, or it may cause the value of the key to be 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.

Five, the internal implementation of the two traversal methods is different.
Hashtable and HashMap all used the iterator. And because of historical reasons, Hashtable also used the enumeration way.

The hash value is different.
Using different hash values, Hashtable directly uses the hashcode of the object. The hash value is recalculated by HashMap.

VII, the internal implementation uses a different array initialization and expansion mode.
Hashtable and HashMap the initial size and expansion of the array of their two internally implemented methods. The default size of the hash array in 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.

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.