Java Collections

Difference between HashMap and HashTable in Java

  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. HashMap does not guarantee that the order of the map will remain constant over time.
  3. HashMap is non synchronized whereas Hashtable is synchronized.
  4. Iterator in the Hashtable is fail-safe because enumerator for the Hashtable is not 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.

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 lock to be released.
  2. Fail-safe is relevant from the context of iterators. If an iterator 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 map.
Parameter Hashmap Hashtable
Synchronized Not Syncronized Syncronized
Null key and Value Allows one null key and any number of null values Not allow null keys or values
Fail fast Iterator in Hashmap is fail fast Enumeration in Hashtable is not fail fast
ThreadSafe No Yes
Extends Extends AbstractMap class Extends Dictionary class
Performance Faster then Hashtable Slower then Hashmap

Note – You can synchonized a HashMap with the help of Collections.synchonizedMap(hashmap)

Map map=Collections.synchonizedMap(hashmap)

Syncronized →Being synchronized means that operation is thread safe, so only one thread can access the table at a time.

Fail safe – Fail-fast means when you try to modify the content when you are iterating, it will throw ConcurrentModification Exception and fail immediately.

For HashMap Itration:

Set keys = hashMap.keySet();
for (Object key : key) {
    hashMap.put(someObject, someValue); //it will throw the ConcurrentModificationException
}

For HashTable Enumeration:

Enumeration keys = hashTable.keys();
 for (Enumeration e = v.elements() ; e.hasMoreElements() ; e.nextElement()) {
      hashTable.put(someKey, someValue); //it works
 }

<<Previous <<   || Index ||   >>Next >>

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago