<div dir="ltr" style="text-align: justify;" trbidi="on">
<ol>
<li>The <b>HashMap </b>class is roughly equivalent to <b>Hashtable</b>, except that it is non synchronized and permits nulls. (<b>HashMap </b>allows null values as key and value whereas <b>Hashtable </b>doesn&#8217;t allow nulls).</li>
<li><b>HashMap </b>does not guarantee that the order of the map will remain constant over time.</li>
<li><b>HashMap </b>is non synchronized whereas <b>Hashtable </b>is synchronized.</li>
<li><b>Iterator in the ;<b><i>Hashtable ;</i></b>is fail-safe</b> ;because ;<b>enumerator for the <i>Hashtable </i>is not throw <i>ConcurrentModificationException </i></b>if any other Thread modifies the map structurally by adding or removing any element except <i><b>Iterator</b></i>&#8216;s own <b>remove()</b> method. But this is not a guaranteed behavior and will be done by <b>JVM </b>on best effort.</li>
</ol>
<p><b>Note on Some Important Terms</b></p>
<p></p>
<ol>
<li>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 <b>hashtable </b>will have to acquire a lock on the object while others will wait for lock to be released.</li>
<li>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 &#8220;structurally&#8221;, a concurrent modification exception will be thrown. It is possible for other threads though to invoke &#8220;set&#8221; method since it doesn&#8217;t modify the collection &#8220;structurally&#8221;. However, if prior to calling &#8220;set&#8221;, the collection has been modified structurally, &#8220;<i><b>IllegalArgumentException</b></i>&#8221; will be thrown.</li>
<li>Structurally modification means deleting or inserting element which could effectively change the structure of map.</li>
</ol>
<div align='center' id="ads-id"></div>
<table cellspacing="0" class="gridtable" style="-webkit-box-shadow: rgba(0, 0, 0, 0.2) 0px 0px 5px; -webkit-text-stroke-width: 0px; background-color: white; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-spacing: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border: 1px solid rgb(221, 221, 221); box-shadow: rgba(0, 0, 0, 0.2) 0px 0px 5px; color: #111111; font-family: OpenSans, 'Trebuchet MS', 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', 'Ubuntu Beta', UbuntuBeta, Ubuntu, 'Bitstream Vera Sans', 'DejaVu Sans', Tahoma, sans-serif; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; margin: 0px 0px 8px; orphans: auto; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<thead style="margin: 0px; padding: 0px;">
<tr style="margin: 0px; padding: 0px;">
<th style="-webkit-box-shadow: rgba(255, 255, 255, 0.8) 0px 1px 0px inset; background-color: #f8f8f8; background-image: -webkit-linear-gradient(top, rgb(238, 238, 238), rgb(248, 248, 248)); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-left-style: none; border-top-left-radius: 4px; border-top-right-radius: 0px; border-top-width: 0px; box-shadow: rgba(255, 255, 255, 0.8) 0px 1px 0px inset; margin: 0px; padding: 5px; text-align: left; text-shadow: rgba(255, 255, 255, 0.498039) 0px 1px 0px;" width="20%">Parameter</th>
<th style="-webkit-box-shadow: rgba(255, 255, 255, 0.8) 0px 1px 0px inset; background-color: #f8f8f8; background-image: -webkit-linear-gradient(top, rgb(238, 238, 238), rgb(248, 248, 248)); border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-top-width: 0px; box-shadow: rgba(255, 255, 255, 0.8) 0px 1px 0px inset; margin: 0px; padding: 5px; text-align: left; text-shadow: rgba(255, 255, 255, 0.498039) 0px 1px 0px;" width="40%">Hashmap</th>
<th style="-webkit-box-shadow: rgba(255, 255, 255, 0.8) 0px 1px 0px inset; background-color: #f8f8f8; background-image: -webkit-linear-gradient(top, rgb(238, 238, 238), rgb(248, 248, 248)); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-top-left-radius: 0px; border-top-right-radius: 4px; border-top-width: 0px; box-shadow: rgba(255, 255, 255, 0.8) 0px 1px 0px inset; margin: 0px; padding: 5px; text-align: left; text-shadow: rgba(255, 255, 255, 0.498039) 0px 1px 0px;" width="40%">Hashtable</th>
</tr>
</thead>
<tbody style="margin: 0px; padding: 0px;">
<tr style="margin: 0px; padding: 0px;">
<td style="border-left-style: none; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;"><b style="margin: 0px; padding: 0px;">Synchronized</b></td>
<td style="border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;">Not Syncronized</td>
<td style="border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;">Syncronized</td>
</tr>
<tr style="-webkit-box-shadow: rgba(255, 255, 255, 0.8) 0px 1px 0px inset; background-color: #f8f8f8; background-position: initial initial; background-repeat: initial initial; box-shadow: rgba(255, 255, 255, 0.8) 0px 1px 0px inset; margin: 0px; padding: 0px;">
<td style="border-left-style: none; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;"><b style="margin: 0px; padding: 0px;">Null key and Value</b></td>
<td style="border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;">Allows one null key and any number of null values</td>
<td style="border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;">Not allow null keys or values</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-left-style: none; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;"><b style="margin: 0px; padding: 0px;">Fail fast</b></td>
<td style="border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;">Iterator in Hashmap is fail fast</td>
<td style="border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;">Enumeration in Hashtable is not fail fast</td>
</tr>
<tr style="-webkit-box-shadow: rgba(255, 255, 255, 0.8) 0px 1px 0px inset; background-color: #f8f8f8; background-position: initial initial; background-repeat: initial initial; box-shadow: rgba(255, 255, 255, 0.8) 0px 1px 0px inset; margin: 0px; padding: 0px;">
<td style="border-left-style: none; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;"><b style="margin: 0px; padding: 0px;">ThreadSafe</b></td>
<td style="border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;">No</td>
<td style="border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;">Yes</td>
</tr>
<tr style="-webkit-transition: all 0.1s ease-in-out; background-color: #ffffdf; background-position: initial initial; background-repeat: initial initial; margin: 0px; padding: 0px; transition: all 0.1s ease-in-out;">
<td style="border-left-style: none; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;"><b style="margin: 0px; padding: 0px;">Extends</b></td>
<td style="border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;">Extends AbstractMap class</td>
<td style="border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;">Extends Dictionary class</td>
</tr>
<tr style="-webkit-box-shadow: rgba(255, 255, 255, 0.8) 0px 1px 0px inset; background-color: #f8f8f8; background-position: initial initial; background-repeat: initial initial; box-shadow: rgba(255, 255, 255, 0.8) 0px 1px 0px inset; margin: 0px; padding: 0px;">
<td style="border-bottom-left-radius: 4px; border-bottom-right-radius: 0px; border-left-style: none; border-top-color: rgb(221, 221, 221); border-top-left-radius: 0px; border-top-right-radius: 0px; border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;"><b style="margin: 0px; padding: 0px;">Performance</b></td>
<td style="border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-top-color: rgb(221, 221, 221); border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;">Faster then Hashtable</td>
<td style="border-bottom-left-radius: 0px; border-bottom-right-radius: 4px; border-left-color: rgb(221, 221, 221); border-left-style: solid; border-left-width: 1px; border-top-color: rgb(221, 221, 221); border-top-left-radius: 0px; border-top-right-radius: 0px; border-top-style: solid; border-top-width: 1px; margin: 0px; padding: 5px; text-align: left;">Slower then Hashmap</td>
</tr>
</tbody>
</table>
<p><b>Note &#8211; You can <i>synchonized </i>a <i>HashMap </i>with the help of <i>Collections.synchonizedMap(hashmap)</i></b></p>
<pre class="highlight" name="code">Map map=Collections.synchonizedMap(hashmap) 
</pre>
<p><b>Syncronized </b>→Being synchronized means that operation is thread safe, so only one thread can access the table at a time.</p>
<p> <b>Fail safe</b> &#8211; Fail-fast means when you try to modify the content when you are iterating, it will throw <i><b>ConcurrentModification </b></i>Exception and fail immediately.</p>
<p> <b>For <i>HashMap </i>Itration:</b></p>
<pre class="highlight" name="code">Set keys = hashMap.keySet(); 
for (Object key : key) { 
 hashMap.put(someObject, someValue); //it will throw the ConcurrentModificationException 
} 
</pre>
<p><b>For <i>HashTable </i>Enumeration:</b></p>
<pre class="highlight" name="code">Enumeration keys = hashTable.keys(); 
 for (Enumeration e = v.elements() ; e.hasMoreElements() ; e.nextElement()) { 
 hashTable.put(someKey, someValue); //it works 
 } 
</pre>
<p></p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<b>Java Collections Tutorial</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/arraylist-class-in-java-collection/">ArrayList class</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedlist-class-in-collection/">LinkedList class</a></b></li>
<li><b><a href="https://dineshonjava.com/listiterator-interface-in-collection/">ListIterator interface</a></b></li>
<li><b><a href="https://dineshonjava.com/hashset-class-in-collection/">HashSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedhashset-class-in-collection/">LinkedHashSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/treeset-classin-collection/">TreeSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-hashset-and-treeset/">Difference between TreeSet &; HashSet</a></b></li>
<li><b><a href="https://dineshonjava.com/map-interface-in-collection/">Map interface</a></b></li>
<li><b><a href="https://dineshonjava.com/hashmap-class-in-collection-framework/">HashMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/how-does-java-hashmap-work-internally/">How does work HashMap?</a> </b></li>
<li><b><a href="https://dineshonjava.com/difference-between-treemap-vs-hashmap/">Difference between TreeMap vs HashMap</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedhashmap-class-in-collection/">LinkedHashMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/treemap-class-in-collection-framework/">TreeMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/hashtable-class-in-collection-framework/">Hashtable class</a></b></li>
<li><b><a href="https://dineshonjava.com/sorting-in-collection-framework/">Sorting</a></b></li>
<li><b><a href="https://dineshonjava.com/java-comparable-and-comparator/">Comparable interface</a></b></li>
</ol>
</div>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/hashtable-class-in-collection-framework/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/sorting-in-collection-framework/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/hashtable-class-in-collection-framework/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/sorting-in-collection-framework/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '408'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…