The difference between Hashtable and HashMap

Source: Internet
Author: User

??

Summarize

??

One important difference between these two things is that Hashtable is thread-safe, and HashMap is thread insecure.

??

HashMap to achieve synchronization through an additional synchronization mechanism: a static method of the general collections is resolved: Map m = collections.synchronizedmap (new HashMap (...));

??

This method returns a synchronized map that encapsulates all the methods of the underlying hashmap, making the underlying hashmap secure even in multi-threaded environments.

??

In addition Hashtable key and value are not null, and HashMap can,HashMap can be used Null is a key (of course there can be only one), and a null value is allowed, which can be used by one or more keys to correspond to a null value.

??

Another important difference is that Hashtable inherits from the dictionary class and HashMap inherits from the map interface.

??

Test

??

For example, I also add 2,000,000 the same string to the two data structures to compare the time of its insertion

??

117 Millis have passed when using HashTable.

Millis have passed when using HASHMAP.

??

Test code

??

public class Hashmapandhashtable {

??

private static final String base = "base string.";

private static final int count = 2000000;

??

public static void Hashmaptest () {

String key= "Hello";

String value= "World";

Long begin, end;

Begin = System.currenttimemillis ();

Hashmap<string, string> hashmap=new hashmap<> ();

for (int i = 0; i < count; i++) {

Hashmap.put (key, value);

}

End = System.currenttimemillis ();

System.out.println ((End-begin)

+ "Millis have passed when using HashMap.");

}

??

public static void Hashtabletest () {

String key= "Hello";

String value= "World";

Long begin, end;

Begin = System.currenttimemillis ();

Hashtable<string, String>hashtable=new hashtable<> ();

for (int i = 0; i < count; i++) {

Hashtable.put (key, value);

}

End = System.currenttimemillis ();

System.out.println ((End-begin)

+ "Millis have passed when using HashTable.");

}

public static void Main (string[] args) {

TODO auto-generated Method Stub

Hashtabletest ();

Hashmaptest ();

}

??

}

??

The difference between Hashtable and HashMap

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.