What is Difference Between HashMap and Hashtable in Java?
Here we will discuss, What is difference between HashMap and Hashtable in java. This is the most frequently asked question in any java interviews.
So let's start, What is the difference between hashmap and hashtable class?
Java HashMap
- HashMap is non-synchronized i.e multiple threads can access it simultaneously. It is not a thread-safe.
- HashMap allows one null key and multiple null values.
- HashMap extends AbstractMap class.
- HashMap provides fast performance.
- HashMap is a new class and introduced in jdk 1.2.
- HashMap returns the only iterator to traverse.
- The iterator of HashMap is fail-fast and it throws ConcurrentModificationException.
- It is good for single threaded applications but if you want to use HashMap class in multithreaded applications, You will have to use Collections.synchronizedMap(hashMap).
Java HashMap Example
This is the simple example of hash map class where we will insert some key and values and then traverse all the elements from the map.
import java.util.*;
class Demo
{
public static void main(String args[])
public static void main(String args[])
{
//declaring HashMap
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(1000, "suman");//inserting key and value
hm.put(3000, "hari");
hm.put(99, "pooja");
hm.put(5000, "kiran");
hm.put(20000, "bheem");
for(Map.Entry m : hm.entrySet())
{
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
Output: 20000 bheem
99 pooja
1000 suman
3000 hari
5000 kiran
Note: HashMap class contains only unique elements i.e not duplicate elements and doesn't maintain insertion order of an element.
Java Hashtable
- Hashtable is a synchronized i.e multiple threads can access it one by one. It is thread-safe.
- Hashtable doesn't allow any null key and null values.
- Hashtable extends Dictionary class.
- Hashtable provides slow performance.
- Hashtable is a legacy class.
- Hashtable returns both Iterator as well as Enumeration for traversal.
- Enumeration for Hashtable is not fail-fast.
- It is good for the multithreaded environment.
Hashtable Example in Java
This is java hashtable example Where we will insert some elements and then traverse all the elements from the table.
import java.util.*;
class Test
{
public static void main(String args[])
{
//declaring Hashtable
Hashtable<Integer, String> ht = new Hashtable<Integer, String>();
ht.put(20, "red");//inserting elements
ht.put(10, "black");
ht.put(50, "yellow");
ht.put(7, "pink");
ht.put(90, "brown");
for(Map.Entry m : ht.entrySet())
{
System.out.println(m.getKey()+" "+m.getValue());
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
Output: 10 black
20 red
7 pink
50 yellow
90 brown
Note: Hashtable class contains only unique elements i.e not duplicate elements and it doesn't maintain any insertion order of an element.
Output: 10 black
20 red
7 pink
50 yellow
90 brown
Note: Hashtable class contains only unique elements i.e not duplicate elements and it doesn't maintain any insertion order of an element.
So there are many differences between HashMap and Hashtable in java and you can see other differences topics in collection framework like Difference between ArrayList and LinkedList, ArrayList and Vector, etc.
Similarities Between HashMap and Hashtable class
- Both implements Map interface.
- Both don't maintain insertion order of an element.
- Both store the data in the form of key and value pairs.
- Both using the hashing technique to store the key & value pairs.
- Both contain only unique elements i.e cannot allow duplicate elements
Really very helpful article.
ReplyDeleteGreat Article android based projects
ReplyDeleteJava Training in Chennai
Project Center in Chennai
Java Training in Chennai
projects for cse
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training