top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is difference between HashMap and HashTable?

0 votes
569 views
What is difference between HashMap and HashTable?
posted Dec 16, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

HashMap

1) HashMap is non synchronized. It is not-thread safe and can't be shared between many threads without proper synchronization code.
2) HashMap allows one null key and multiple null values.
3) HashMap is a new class introduced in JDK 1.2.
4) HashMap is fast.
5) We can make the HashMap as synchronized by calling this code
Map m = Collections.synchronizedMap(hashMap);
6) HashMap is traversed by Iterator.
7) Iterator in HashMap is fail-fast.
8) HashMap inherits AbstractMap class.

Hashtable

1) Hashtable is synchronized. It is thread-safe and can be shared with many threads.
2) Hashtable doesn't allow any null key or value.
3) Hashtable is a legacy class.
4) Hashtable is slow.
5) Hashtable is internally synchronized and can't be unsynchronized.
6) Hashtable is traversed by Enumerator and Iterator.
7) Enumerator in Hashtable is not fail-fast.
8) Hashtable inherits Dictionary class.

answer Dec 17, 2015 by Shivaranjini
0 votes

There are many differences between these two classes, some of them are following:-

  1. Hashtable is a legacy class and present from JDK 1, HashMap was added later.
  2. Hashtable is synchronized and slower but HashMap is not synchronized and faster.
  3. Hashtable doesn't allow null keys but HashMap allows one null key.
  4. Hashtable is a thread-safe collection while HashMap is not thread-safe.
  5. Hashtable is obsolete class and you should be using ConcurrentHashMap in place of Hashtable in Java.
answer Oct 12, 2016 by Ramya
Similar Questions
+1 vote

Example
Map data = new HashMap();
data.put("a","America");
data.put("a","Africa");
data.put("b","Bangladesh");

Need to have both "America" and "Africa" as value against key "a". Is there any mechanism to achieve this scenario.

...