Difference Between Similar Terms and Objects

Difference Between HashMap and Hashtable

The Java Collections framework provides a set of collection classes. Each class has its own performance strengths and weaknesses. Some of the classes provide full implementations that can be used as-is. Others are abstract that provide skeletal implementations that are used as starting points for creating collections. The collection implementations use the synchronized-wrapper framework to provide synchronized classes, otherwise the implementations are unsynchronized. Several classes provide implementations of the map interface.

The Java platform contains three general-purpose Map implementations: HashMap, TreeMap, and LinkedHashMap. HashMap and Hashtable are two collections in Java used to store key/value pairs in a hash table. Hashtable is a synchronized Map and HashMap is an unsynchronized Map. Nevertheless, if you need to use a synchronized Map, a Hashtable is faster than using a HashMap in a synchronized wrapper. Both are hash-based collections in Java but they have their fair share of differences. We highlight some key differences between the two to help you better understand the terms.

 

What is HashMap?

HashMap is a Map implementation based on a hash table which provides constant-time performance for inserting and locating pairs. The HashMap class provides a map implementation that is based on a Hashtable data structure. This implementation supports all Map operations and allows multiple null values but only one null key. It uses key/value pairs to store values in a hash table. It is an unsynchronized Map which means it is not thread-safe and cannot be shared between multiple threads without proper synchronization.

What is Hashtable?

Unlike HashMap, Hashtable is a synchronized Map and it is thread-safe which means it can be shared between multiple threads. In Hashtable, you specify an object that can be used as a key and the value that goes with the key. A Hashtable maps keys to values with the help of a hash function. Java provides this function in the form of Object’s hashcode( ) method, which classes override to provide appropriate hash codes. Unlike HashMap, Hashtable does not support null values and null keys because there is null check in the put method implementation of Hashtable.

 

Difference between HashMap and Hashtable

  1. Basics of HashMap Vs. Hashtable

Both are hash-based collections in Java used to store data in key/value pairs. HashMap is a Map implementation based on a hash table which provides constant-time performance for inserting and locating pairs. Performance can be adjusted with the use of constructors that allow you to set the capacity and load factor of the hash table. The basic Hashtable is quite similar to the HashMap, even down the method names. It stores key/value pair in hash table. In Hashtable, you specify an object that can be used as a key and the value that goes with the key.

  1. Synchronization of HashMap Vs. Hashtable

Both HashMap and Hashtable use hashing techniques to store values based on the key. Like HashMap, Hashtable uses key/value pairs to store values in a hash table. However, the key difference between the two is synchronization. HashMap is an unsynchronized Map whereas Hashtable is a synchronized Map. This means HashMap is not thread-safe and cannot be shared between multiple threads without proper synchronization code. On the contrary, Hashtable is thread-safe and can be shared between multiple threads. Hashtable is faster than using a HashMap in a synchronized wrapper, if you need to use a synchronized Map.

  1. Null Keys and Null Values for HashMap Vs. Hashtable

The HashMap class provides a map implementation that is based on a Hashtable data structure. This implementation supports all Map operations and allows multiple null values but only one null key so that it could maintain unique key properties. However, it makes no guarantees on the order in which the entries are stored. The Hashtable, on the other hand, maps keys to values with the help of a hash function. Unlike HashMap, Hashtable does not support null values and null keys because there is null check in the put method implementation of Hashtable.

  1. Performance of HashMap Vs. Hashtable

Because HashMap is not a synchronized Map it is much faster and better than a Hashtable in terms of performance, and in fact, uses less memory than Hashtable. Although they are virtually identical, Hashtable is a bit slower than a HashMap but faster than a synchronized HashMap. Inherently, it is not safe to use Hashtable with multithreaded access because only the methods are synchronized. Hashtable is the synchronized counterpart to HashMap. Unsynchronized objects perform better when compared to synchronized objects just like a Hashtable performs better in a single threaded environment.

HashMap vs. Hashtable: Comparison Chart

 

 

Summary of HashMap Vs. Hashtable

The HashMap class gives you an unsorted, unordered Map. So when you need a Map and you don’t care about the order in which the entries are stored, then HashMap is the way to go.

Hashtable, just like Vector, has been in from prehistoric Java times. Just like Vector is a synchronized counterpart to the more modern and advanced ArrayList, Hashtable is the synchronized counterpart to HashMap. However, a class cannot be synchronized, so when we say that Hashtable is a synchronized Map, it means that the key methods of the class are synchronized. 

While both are virtually identical, the difference lies in the way they are synchronized and how they perform. HashMap performs better in a multi threaded environment whereas Hashtable performs better in a single threaded environment.

Latest posts by Sagar Khillar (see all)

Sharing is caring!


Search DifferenceBetween.net :




Email This Post Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.


Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

References :


[0]Image credit: https://upload.wikimedia.org/wikipedia/commons/thumb/7/7b/Java.util.Map_hierarchy.svg/2000px-Java.util.Map_hierarchy.svg.png

[1]Image credit: https://upload.wikimedia.org/wikipedia/commons/thumb/d/d0/Hash_table_5_0_1_1_1_1_1_LL.svg/500px-Hash_table_5_0_1_1_1_1_1_LL.svg.png

[2]Sierra, Katherine. All In One Java 2. New Delhi: Dreamtech Press, 2003. Print

[3]Friesen, Jeff. Beginning Java 7. NYC: Apress, 2012. Print

[4]Shirazi, Jack. Java Performance Tuning. Sebastopol, California: O'Reilly Media, 2003. Print

[5]Eckel, Bruce. Thinking in Java. New Jersey: Prentice Hall, 2003. Print

Articles on DifferenceBetween.net are general information, and are not intended to substitute for professional advice. The information is "AS IS", "WITH ALL FAULTS". User assumes all risk of use, damage, or injury. You agree that we have no liability for any damages.


See more about : ,
Protected by Copyscape Plagiarism Finder