A brief analysis of the difference between map and Hashmap,hashtable,hashset in Java (reprint)

Source: Internet
Author: User

Tags: style io ar using SP java strong on Div

 The HashMap and Hashtable two classes implement the map interface, which holds the k-v pair (Key-value pair), and HashSet implements the set interface, which is similar in nature to a set

Hashtable and HashMap differences

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


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 non-synchronize by default. In the multi-threaded concurrency environment, you can use Hashtable directly, do not need to implement its own method of synchronization, but the use of hashmap you have to increase the synchronization process.

Thirdly, 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 contains and Containsvalue function the same.

IV, whether key and value allow null values.
Where key and value are objects and cannot contain duplicate keys, but can contain duplicate value.
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, the key may not be in HashMap, or the value corresponding to the key may be 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.

Five, 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.

Six, the hash value is different.
Hash values are used differently, Hashtable directly using the object's hashcode. The hash value is recalculated by the HashMap.

VII, the internal implementation uses the same array initialization and expansion methods.
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.

A brief analysis of the difference between map and Hashmap,hashtable,hashset in Java (reprint)

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.

Tags Index: