In previous article, I have mentioned Servlet in Java with example (Saving form data in database using servlet) and How to compare Strings in Java? but in this article, I have provided detailed difference between popular collections of java that is Hashmap and Hashtable, but before going through difference, let's take understand each of them first.
Brief Introduction of HashMap and HashTable
HashMap and Hashtable store key/value pairs in a hash table. When using a Hashtable or HashMap, we specify an object that is used as a key, and the value that you want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.
Despite being hash based and similar in functionality there are a significant difference between Hashtable and HashMap and without understanding those difference if you use Hashtable in place of HashMap than you may run into series of subtle programs which is hard to find and debug.
Now, What is the difference between HasMap and HashTable, this is one of the common questions in Interview for Java/J2EE developers, so here i am going to explain you about it to make your concepts about it clear.
Understanding Hashmap and Hashtable one by one (with Difference)
Let's discuss each of them one by one:
HashMap
Java HashMap is one of the most popular Collection classes in java. Java HashMap is Hashtable based implementation. HashMap in java extends AbstractMap class that implements Map interface.
- HashMap is non-synchronized. So it can be used in the multi-threaded environment, ie., more than one thread can access and use it simultaneously.
- HashMap allows null values as key and value
- HashMap is generally preferred over HashTable if thread synchronization is not needed.
Iterator
in theHashMap
is fail-fast and throwConcurrentModificationException
if any other Thread modifies the map structurally by adding or removing any element except Iterator’s own remove() method. But this is not a guaranteed behavior and will be done by JVM on best effort.- Example:
import java.util.*;
class TestingCollection{
public static void main(String args[]){
HashMap<Integer,String> hm=new HashMap<Integer,String>();
hm.put(100,"Raj");
hm.put(101,"Manveer");
hm.put(102,"Vikram");
for(Map.Entry m:hm.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
Output of the above code will be
102 Raj
100 Manveer
101 Vikram
HashTable
Java Hashtable class implements a hashtable, which maps keys to values. It inherits Dictionary class and implements the Map interface.A Hashtable is an array of a list, it contains values based on the key & contains only unique elements.
- HashTable is thread safe and synchronized, it should be used in multithreading applications.
- It doesn't allow null keys and null values in the HashTable object.
- Preferred when thread synchronization is needed.
- HashTable is the only class other than vector which uses the enumerator to iterate the values of HashTable object, the enumerations returned by the Hashtable keys and elements methods are not fail fast.
- Example:
import java.util.*; class TestingCollectionHashTable{ public static void main(String args[]){ Hashtable<Integer,String> ht=new Hashtable<Integer,String>(); ht.put(101,"Amit"); ht.put(101,"Aman"); ht.put(102,"Vikram"); ht.put(103,"Vinay"); for (Map.Entry m:ht.entrySet()) { System.out.println(m.getKey()+" "+m.getValue()); } } } ?
Output:
103 Vinay 102 Vikram 101 Aman?
Quick Overview of difference between HashMap & HashTable
- The
HashMap
class is roughly equivalent toHashtable
, except that it is not synchronized and permits nulls. (HashMap allows null values as key and value whereasHashtable
doesn't allow nulls). HashMap
does not guarantee that the order of the map will remain constant over time.HashMap
is not synchronized whereasHashtable
is synchronized.- Iterator in the
HashMap
is fail-safe while the enumerator for theHashtable
is not and throw ConcurrentModificationException if any other Thread modifies the map structurally by adding or removing any element except Iterator's own remove() method. But this is not a guaranteed behavior and will be done by JVM on best effort. Hashtable
is considered as legacy class whileHashMap
was introduced in JDK 1.2.HashMap
inherits AbstractMap class whileHashtable
inherits Dictionary class.HashMap
is traversed by Iterator whileHashtable
is traversed by Enumerator and Iterator.HashMap
is fast, whileHashtable
is slow