The difference between the Hashtable and the HashMap class

Source: Internet
Author: User

There are three important differences between the Hashtable and the HashMap class. The first difference is mainly historical reasons. Hashtable is based on the old dictionary class, HashMap is an implementation of the map interface introduced by Java 1.2.
  
Perhaps the most important difference is that the Hashtable method is synchronous, and the HashMap method is not. This means that while you can use a hashtable in a multithreaded application without taking any special action, you have to provide an external synchronization for a hashmap as well. A convenient way is to use the static Synchronizedmap () method of the collections class, which creates a thread-safe map object and returns it as a encapsulated object. The method of this object allows you to synchronize access to potential hashmap. The result is that when you don't need to sync, you can't cut off synchronization in Hashtable (like in a single-threaded application), and synchronization adds a lot of processing overhead.
  
The 3rd difference is that only hashmap can let you use NULL as a key or value for the entry of a table. Only one record in HashMap can be an empty key, but any number of entries can be empty value. This means that if a search key is not found in the table, or if a search key is found, but it is an empty value, then get () returns NULL. If necessary, use the Containkey () method to differentiate between the two cases.
  
Some data suggest, when need to synchronize, use Hashtable, converse with HashMap. However, because HashMap can be synchronized when needed, HashMap functions more than Hashtable, and it is not based on a stale class, it is argued that hashmap in all cases takes precedence over Hashtable.
  
About properties
Sometimes, you might want to use a hashtable to map the string of key to the string of value. The environment strings in DOS, WINDOWS, and Unix have some examples, such as the string path of key that is mapped to the string C:\WINDOWS of value; C:\WINDOWS\SYSTEM. Hashtables is a simple way to represent these, but Java provides another way.
  
The Java.util.Properties class is a subclass of Hashtable, designed for string keys and values. The use of the properties object is similar to the use of Hashtable, but the class adds two time-saving methods, which you should know.
  
The store () method saves the contents of a Properties object to a file in a readable form. The Load () method is exactly the opposite, used to read the file and set the Properties object to contain keys and values.
  
Note that because properties extends the Hashtable, you can use the put () method of the superclass to add keys and values that are not string objects. This is not advisable. In addition, if you use the store () for a Properties object that does not contain a string object, store () will fail. As an alternative to put () and get (), you should use SetProperty () and GetProperty (), which use the string parameter.

The difference between the Hashtable and the HashMap class

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.