What’s distinction between HashMap and Hashtable in Java?

[ad_1]

HashMap vs Hashtable in Java
Although each Hashtable and HashMap are data-structure primarily based upon hashing and implementation of Map interface, the principle distinction between them is that HashMap is just not thread-safe however Hashtable is thread-safe. This implies you can not use HashMap in a multi-threaded Java software with out exterior synchronization. One other distinction is HashMap permits one null key and null values however Hashtable would not permit null key or values. Additionally, the thread-safety of the hash desk is achieved utilizing inner synchronization, which makes it slower than HashMap.

By the way in which, the distinction between HashMap and Hashtable in Java is among the incessantly requested in core Java interviews to test whether or not the candidate understands the right utilization of assortment lessons and conscious of other options accessible.

Together with How HashMap internally works in Java and ArrayList vs Vector, this is among the oldest questions from the Assortment framework in Java. Hashtable is a legacy Assortment class and it is there in Java API for a very long time nevertheless it received refactored to implement Map interface in Java 4 and from there Hashtable grew to become a part of the Java Assortment framework.


Hashtable vs HashMap in Java is so in style a query that it may prime any checklist of Java Assortment Interview Questions. You simply cannot afford to organize HashMap vs Hashtable earlier than going to any Java programming interview. On this Java article, we won’t solely see some essential variations between HashMap and Hashtable but in addition some similarities between these two assortment lessons.  Although for full preparation,

Let’s first see How totally different they’re:

HashMap vs Hashtable in Java

Distinction between HashMap and Hashtable in Java

Each HashMap and Hashtable implements Map interface however there may be some important distinction between them which is essential to recollect earlier than deciding whether or not to make use of HashMap or Hashtable in Java. A few of them are thread-safety, synchronization, and pace. listed below are these variations :

1.The HashMap class is roughly equal to Hashtable, besides that it’s non-synchronized and permits nulls. (HashMap permits null values as key and worth whereas Hashtable would not permit nulls).
2. One of many main variations between HashMap and Hashtable is that HashMap is non-synchronized whereas Hashtable is synchronized, which implies Hashtable is thread-safe and may be shared between a number of threads however HashMap cannot be shared between a number of threads with out correct synchronization. Java 5 introduces ConcurrentHashMap which is another of Hashtable and offers higher scalability than Hashtable in Java.

3. One other important distinction between HashMap vs Hashtable is that Iterator within the HashMap is  a fail-fast iterator  whereas the enumerator for the Hashtable is just not and throw ConcurrentModificationException if some other Thread modifies the map structurally  by including or eradicating any component besides Iterator‘s personal take away() technique. However this isn’t a assured habits and might be carried out by JVM on greatest effort. That is additionally an essential distinction between Enumeration and Iterator in Java.

4. Another notable distinction between Hashtable and HashMap is that due to thread-safety and synchronization Hashtable is far slower than HashMap if utilized in Single threaded setting. So for those who do not want synchronization and HashMap are solely utilized by one thread, it outperforms Hashtable in Java.

5. HashMap doesn’t assure that the order of the map will stay fixed over time.

If you’re making ready this query as a part of your Java interview preparation,  I recommend making ready each essential subject as given in Programming Interviews Uncovered. It covers fundamentals, core java, threads, a framework like Spring and Hibernate and plenty of others key subjects.

Hashtable vs HashMap in Java

HashMap and Hashtable : observe on Some Vital Phrases

1)Synchronized means just one Thread can modify a hash desk at one level of time. Mainly, it signifies that any thread earlier than performing an replace on a Hashtable must purchase a lock on the thing whereas others will await the lock to be launched.

2) Fail-safe is related from the context of iterators. If an Iterator or ListIterator has been created on a group object and another thread tries to switch the gathering object “structurally”, a concurrent modification exception might be thrown. It’s potential for different threads although to invoke “set” technique because it would not modify the gathering “structurally“. Nonetheless, if previous to calling “set”, the gathering has been modified structurally, “IllegalArgumentException” might be thrown.
Difference between HashMap and Hashtable in Java

3) Structurally modification means deleting or inserting component which may successfully change the construction of the map.

HashMap may be synchronized by

Map m = Collections.synchronizeMap(hashMap);

Additionally, right here is yet another desk which reveals extra variations between HashMap and Hashtable in Java:

In Abstract, there are important variations between Hashtable and HashMap in Java e.g. thread-safety and pace and primarily based upon that solely use Hashtable for those who completely want thread-safety if you’re operating Java 5 think about using ConcurrentHashMap in Java.

Different Java Assortment tutorials chances are you’ll like
kind a Map by keys and values in Java? (tutorial)
kind an ArrayList in ascending and descending order in Java? (tutorial)

Distinction between ArrayList and HashSet in Java? (reply)
The distinction between TreeMap and TreeSet in Java? (reply)
The distinction between HashMap and ConcurrentHashMap in Java? (reply)
The distinction between HashMap and LinkedHashMap in Java? (reply)
The distinction between Hashtable and HashMap in Java? (reply)
The distinction between HashSet and TreeSet in Java? (reply)
The distinction between ArrayList and LinkedList in Java? (reply)
The distinction between Vector and ArrayList in Java? (reply)
Distinction between EnumMap and HashMap in Java

Thanks for studying this text thus far. Should you like this text then please share with your folks and colleagues. In case you have any query or suggestions then please drop a remark.



[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *