Lewis D.
—In Java, both HashMap
and Hashtable
are data structures that map keys to values. In fact, the two data structures are so alike that in some applications, using either implementation may not make a noticeable difference. We should state at the outset that the Hashtable class has been made obsolete since Java 2 Platform v1.2.
Still, it is important to know when to use HashMap or Hashtable, because even though the two are quite similar, using these data structures improperly could produce undesirable results. Let’s consider some of the fundamental differences between HashMaps and Hashtables and examine why you would avoid Hashtables as a rule of thumb.
The key functional difference between HashMap and Hashtable is that a Hashtable is synchronized by default. This means that two or more threads may modify the data structure and will each wait their turn to do so. While this seems beneficial, in practice you will likely need additional synchronization management. Consider the example below:
if (!ht.containsKey("myKey")) { ht.put(key, value); }
This code will require additional synchronization because the Hashtable’s synchronization is per method. This means a race condition could easily occur here between the containsKey()
and put()
method calls. This problem, and many similar issues, are solved in the ConcurrentHashMap implementation, which is a more modern and recommended alternative to the Hashtable.
By comparison, the HashMap data structure has no built-in synchronization and is therefore not safe to use in multithreaded applications. For example, iterating over a HashMap is fail-fast
, which means that an exception will be thrown as soon as modification by another thread is detected. This lack of synchronization does, however, give a significant performance boost over synchronized alternatives in single-threaded implementations.
Another difference between a HashMap and Hashtable is that a Hashtable does not allow keys or values to be set to null
. The HashMap, however, allows any of its values to be null
, as well as one of its keys. Keys still have to be unique, so this is why only one can be null
.
Below is a table that summarizes all the key differences as outlined above. In conclusion, we should always be using the HashMap class where synchronization is not applicable and a single threaded implementation is sufficient. Where multithreading is required, then the best substitution for a Hashtable is a ConcurrentHashMap.
HashMap | Hashtable | |
---|---|---|
Synchronization | Not synchronized, therefore not particularly thread-safe. | Synchronized, can therefore be used in multithreaded implementations. |
Null values | Can have one null key and many null values. | Neither keys nor values may be null. |
Efficiency | Due to no synchronization, it’s faster than the Hashtable and uses less memory. | Much slower than the HashMap. |
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.