What is difference between HashMap and Hashtable in Java?

HashMap vs Hashtable in Java
Though both Hashtable and HashMap are data-structure based upon hashing and implementation of Map interface, the main difference between them is that HashMap is not thread-safe but Hashtable is thread-safe. Which means you cannot use HashMap in multi-threaded Java application without external synchronization. Another difference is HashMap allows one null key and null values but Hashtable doesn't allow null key or values. Also, thread-safety of the hash table is achieved using internal synchronization, which makes it slower than HashMap. By the way,  difference between HashMap and Hashtable in Java  is one of the frequently asked in core Java interviews to check whether the candidate understands correct usage of collection classes and aware of alternative solutions available.

Along with  How HashMap internally works in Java  and  ArrayList vs Vector , this  is one of the oldest questions from Collection framework in Java. Hashtable is a legacy Collection class and it's there in Java API from a long time but it got refactored to implement Map interface in Java 4 and from there Hashtable became part of Java Collection framework.

Hashtable vs HashMap   in Java is so popular a question that it can top any list of  Java Collection interview Question . You just can't afford not to prepare  HashMap  vs  Hashtable  before going to any Java programming interview. In this Java article, we will not only see some important differences between HashMap and Hashtable but also some similarities between these two collection classes.  Though for complete preparation,

Let's first see How different they are:

Difference between HashMap and Hashtable in Java

Both HashMap and Hashtable implements Map interface but there is some significant difference between them which is important to remember before deciding whether to use HashMap or Hashtable in Java. Some of them are thread-safety, synchronization, and speed. here are those differences :

1.The  HashMap class is roughly equivalent to  Hashtable, except that it is non-synchronized and permits nulls. (HashMap allows null values as key and value whereas  Hashtable doesn't allow nulls).

2. One of the major  differences between HashMap and Hashtable is that HashMap is non-synchronized whereas Hashtable is synchronized, which means  Hashtable is thread-safe and can be shared between multiple threads but  HashMap can not be shared between multiple threads without proper synchronization. Java 5 introduces  ConcurrentHashMap which is an alternative of Hashtable and provides better scalability than Hashtable in Java.
                                                         

3. Another significant difference between HashMap vs Hashtable is that  Iterator  in the HashMap is  a fail-fast iterator  while the enumerator for the Hashtable 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. This is also an important  difference between Enumeration and Iterator in Java .

4. One more notable  difference between Hashtable and  HashMap  is that because of thread-safety and synchronization Hashtable is much slower than HashMap if used in Single threaded environment. So if you don't need synchronization and  HashMap  are only used by one thread, it outperforms Hashtable in Java.

5.  HashMap  does not guarantee that the order of the map will remain constant over time.

If you are preparing this question as part of your Java interview preparation,  I suggest preparing every important topic as given in  Programming Interviews Exposed . It covers basics, core java, threads, a framework like Spring and Hibernate and many others key topics.

HashMap and Hashtable : note on Some Important Terms

1)Synchronized means only one Thread can modify a hash table at one point of time. Basically, it means that any thread before performing an update on a  Hashtable will have to acquire a lock on the object while others will wait for the lock to be released.

2) Fail-safe is relevant from the context of iterators. If an  Iterator or ListIterator has been created on a collection object and some other thread tries to modify the collection object "structurally", a concurrent modification exception will be thrown. It is possible for other threads though to invoke "set" method since it doesn't modify the collection " structurally". However, if prior to calling "set", the collection has been modified structurally, " IllegalArgumentException" will be thrown.

3) Structurally modification means deleting or inserting element which could effectively change the structure of the map.

HashMap can be synchronized by

Map m = Collections.synchronizeMap(hashMap);

In Summary, there are significant  differences between Hashtable and HashMap in Java e.g. thread-safety and speed and based upon that only use Hashtable if you absolutely need thread-safety if you are running Java 5 consider using  ConcurrentHashMap in Java.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值