The difference between HashMap and Hashtable

Source: Internet
Author: User

Guide:

1 HashMap is not thread-safe

HashMap is an interface that is a sub-interface of the map interface, an object that maps keys to values, where the keys and values are objects and cannot contain duplicate keys, but can contain duplicate values. HashMap allows null key and null value, while Hashtable is not allowed.

2 Hashtable is a thread-safe collection.

HashMap is a lightweight implementation of Hashtable (non-thread-safe implementation), they all complete the map interface, the main difference is that the HASHMAP allows null (NULL) key value (key), because of non-thread security, the efficiency may be higher than Hashtable. HashMap allows NULL to be used as a entry key or value, and Hashtable is not allowed. HashMap hashtable contains method removed, changed to Containsvalue and ContainsKey. Because the contains method is easy to cause misunderstanding. Hashtable inherits from the dictionary class, and HashMap is an implementation of the map interface introduced by Java1.2. The biggest difference is that the Hashtable method is synchronize, and HashMap is not, when multiple threads access Hashtable, they do not need to synchronize their methods, and HashMap must provide external synchronization. Hashtable and HashMap use the same hash/rehash algorithm, so the performance will not be much worse

public static void Main (String args[]) {HashTable h=new HashTable (); H.put ("User 1", new Integer); H.put ("User 2", new Intege R (50)); H.put ("User 3", new Integer (60)); H.put ("User 4", New Integer (70)); H.put ("User 5", New Integer (80)); Enumeration e=h.elements (); while (E.hasmoreelements ()) {System.out.println (e.nextelement ());}

Summarize:

hashmap

allow null keys and values

high efficiency,

hashmap is an implementation of the Java1.2 introduced map interface

Hashtable

Thread Safety

Null keys and values are not allowed

A little less efficient,

The method is synchronize.

There are contains method methods

, Hashtable inherit from dictionary class

Hashtable is older than HashMap.

These represent the collection in Java, where the main element is whether the elements are ordered, whether they can be repeated to distinguish between memory, in order to properly use, of course, there are synchronization differences, see the previous article related.

Same Title article: the difference between HashMap and Hashtable _ouchuquan space _ Baidu Space

Reprint: http://www.cnblogs.com/langtianya/archive/2013/03/19/2970273.html

The difference between HashMap and Hashtable

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.