{"id":4191,"date":"2019-05-10T13:07:56","date_gmt":"2019-05-10T17:07:56","guid":{"rendered":"http:\/\/springframework.guru\/?p=4191"},"modified":"2024-10-19T11:04:40","modified_gmt":"2024-10-19T15:04:40","slug":"java-hashmap-vs-hashtable","status":"publish","type":"post","link":"https:\/\/springframework.guru\/java-hashmap-vs-hashtable\/","title":{"rendered":"Java HashMap vs Hashtable"},"content":{"rendered":"\n<p>In Java, the HashMap and HashTable are both data structures that store data in key value pairs. When you store data in these, the key value is hashed, then is used as an index for fast access to the stored data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">HashMap vs Hashtable<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>HashMap<\/strong><\/td><td><strong>Hashtable<\/strong><\/td><\/tr><tr><td>Introduced in Java 1.2<\/td><td>Introduced in Java 1.0, considered obsolete with the addition of HashMap in Java 1.2<\/td><\/tr><tr><td>Not synchronized, not thread-safe<\/td><td>Synchronized, thread-safe<\/td><\/tr><tr><td>Faster because it is not synchronized<\/td><td>Slower due to synchronization overhead<\/td><\/tr><tr><td>Will allow one null key, and null values<\/td><td>Will not allow null keys or null values<\/td><\/tr><tr><td>Extends <code>AbstractMap<\/code> and implements <code>Map<\/code> interface<\/td><td>Extends <code>Dictionary<\/code>, implements <code>Map<\/code> Interface<\/td><\/tr><tr><td><\/td><td><\/td><\/tr><tr><td><\/td><td><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">HashMap vs Hashtable<\/figcaption><\/figure>\n\n\n\n<p>Some other key differences are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Because of synchronization and thread safety, <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">Hashtable<\/code> is much slower than <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">HashMap<\/code> if used in single threaded environment.<\/li>\n\n\n\n<li><code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">HashMap<\/code> allows one <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">null<\/code> key and multiple <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">null<\/code> values whereas <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">Hashtable<\/code> doesn&#8217;t allow <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">null<\/code> values.<\/li>\n\n\n\n<li><code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">HashMap<\/code> is traversed by <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">Iterator<\/code> while <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">Hashtable<\/code> can be traversed by both <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">Iterator<\/code> and the legacy <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">Enumeration<\/code>.<\/li>\n\n\n\n<li><code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">Iterator<\/code> in <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">HashMap<\/code> is a fail-fast iterator. It throws <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">ConcurrentModificationException<\/code> if any thread other than the iterator\u2019s <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">remove()<\/code> method tries to modify the map structurally. Thus, in the face of concurrent modification, the iterator fails fast and cleanly, rather than risking undesirable behaviour. However, The <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">Enumeration<\/code> returned by <code data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\" data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">Hashtable<\/code> doesn&#8217;t have this behaviour.<\/li>\n<\/ul>\n\n\n\n<p>In addition to these differences, one commonly asked question is <em>Why <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">HashMap<\/code> stores one null key but <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">Hashtable<\/code> does not?<\/em><\/p>\n\n\n\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">HashMap<\/code>, allows storing one <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">null<\/code> key and multiple <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">null<\/code> values. The reason for allowing only one <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">null<\/code> key is because keys in a <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">HashMap<\/code> has to be unique. On the other hand <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">Hashtable<\/code> does not allow <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">null<\/code> keys. This is because the objects used as keys in a <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">Hashtable<\/code> implements the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">hashCode()<\/code> and <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">equals()<\/code> methods for their storage and retrieval. Since <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">null<\/code> is not an object it cannot implement the methods. If you try hashing a <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">null<\/code> key, it will throw a <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">NullPointerException<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Synchronization with Hashtable<\/h2>\n\n\n\n<p>While <code>Hashtable<\/code> does have built-in synchronization, it may not be enough to ensure complete thread safety. This is because the synchronization is applied at the method level. <\/p>\n\n\n\n<p>For example you might check if a key exists with <code>containsKey()<\/code> before a <code>put()<\/code> operation. Another thread could add the key between method calls, leading to an unexpected exception. <\/p>\n\n\n\n<p>To avoid these inherit limitations of <code>Hashtable<\/code>, you might wish to consider using Java&#8217;s <a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/21\/docs\/api\/java.base\/java\/util\/concurrent\/ConcurrentHashMap.html\" target=\"_blank\" rel=\"noopener\" title=\"\">ConcurrentHashMap<\/a>. Introduced in Java 1.5, the ConcurrentHashMap is thread-safe and performant.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">HashMap Example Usage<\/h2>\n\n\n\n<p>The following example shows how HashMap will accept values and null values for keys and values.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#fff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"@Test\n    void hashMapTest() {\n        HashMap&lt;String, String&gt; hashMap = new HashMap&lt;&gt;();\n        hashMap.put(null, &quot;test&quot;);\n        hashMap.put(&quot;test&quot;, null);\n        hashMap.put(&quot;key1&quot;, &quot;test&quot;);\n        hashMap.put(&quot;key2&quot;, &quot;test2&quot;);\n\n        hashMap.forEach((k, v) -&gt; System.out.println(&quot;Key: &quot; + k + &quot; Value: &quot; + v));\n    }\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #24292E\">@<\/span><span style=\"color: #D73A49\">Test<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #D73A49\">void<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">hashMapTest<\/span><span style=\"color: #24292E\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        HashMap&lt;<\/span><span style=\"color: #D73A49\">String<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #D73A49\">String<\/span><span style=\"color: #24292E\">&gt; hashMap <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">new<\/span><span style=\"color: #24292E\"> HashMap&lt;&gt;();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashMap.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #005CC5\">null<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #032F62\">&quot;test&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashMap.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;test&quot;<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #005CC5\">null<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashMap.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;key1&quot;<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #032F62\">&quot;test&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashMap.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;key2&quot;<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #032F62\">&quot;test2&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashMap.<\/span><span style=\"color: #6F42C1\">forEach<\/span><span style=\"color: #24292E\">((k, v) <\/span><span style=\"color: #D73A49\">-&gt;<\/span><span style=\"color: #24292E\"> System.out.<\/span><span style=\"color: #6F42C1\">println<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;Key: &quot;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">+<\/span><span style=\"color: #24292E\"> k <\/span><span style=\"color: #D73A49\">+<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #032F62\">&quot; Value: &quot;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">+<\/span><span style=\"color: #24292E\"> v));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    }<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Following is the output from the above test.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#fff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"Key: null Value: test\nKey: key1 Value: test\nKey: key2 Value: test2\nKey: test Value: null\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #24292E\">Key<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">null<\/span><span style=\"color: #24292E\"> Value<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> test<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">Key<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> key1 Value<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> test<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">Key<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> key2 Value<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> test2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">Key<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> test Value<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">null<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">HashTable Example Useage<\/h2>\n\n\n\n<p>The following test shows an example shows an example of using <code>Hashtable<\/code>. <\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#fff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"    @Test\n    void hashTableTest() {\n        Hashtable&lt;String, String&gt; hashTable = new Hashtable&lt;&gt;();\n        hashTable.put(&quot;key1&quot;, &quot;test&quot;);\n        hashTable.put(&quot;key2&quot;, &quot;test2&quot;);\n        hashTable.put(&quot;key3&quot;, &quot;test2&quot;);\n\n        hashTable.forEach((k, v) -&gt; System.out.println(&quot;Key: &quot; + k + &quot; Value: &quot; + v));\n    }\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #24292E\">    @<\/span><span style=\"color: #D73A49\">Test<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #D73A49\">void<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">hashTableTest<\/span><span style=\"color: #24292E\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        Hashtable&lt;<\/span><span style=\"color: #D73A49\">String<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #D73A49\">String<\/span><span style=\"color: #24292E\">&gt; hashTable <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">new<\/span><span style=\"color: #24292E\"> Hashtable&lt;&gt;();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashTable.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;key1&quot;<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #032F62\">&quot;test&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashTable.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;key2&quot;<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #032F62\">&quot;test2&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashTable.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;key3&quot;<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #032F62\">&quot;test2&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashTable.<\/span><span style=\"color: #6F42C1\">forEach<\/span><span style=\"color: #24292E\">((k, v) <\/span><span style=\"color: #D73A49\">-&gt;<\/span><span style=\"color: #24292E\"> System.out.<\/span><span style=\"color: #6F42C1\">println<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;Key: &quot;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">+<\/span><span style=\"color: #24292E\"> k <\/span><span style=\"color: #D73A49\">+<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #032F62\">&quot; Value: &quot;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">+<\/span><span style=\"color: #24292E\"> v));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    }<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This will generate the following output:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#fff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"Key: key1 Value: test\nKey: key2 Value: test2\nKey: key3 Value: test2\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #24292E\">Key<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> key1 Value<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> test<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">Key<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> key2 Value<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> test2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">Key<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> key3 Value<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> test2<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Null Values with HashTable<\/h2>\n\n\n\n<p>The following test tries to set a null key with Hashtable. <\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#fff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"    @Test\n    void hashTableWithNullKeyTest() {\n        Hashtable&lt;String, String&gt; hashTable = new Hashtable&lt;&gt;();\n        hashTable.put(&quot;key1&quot;, &quot;test&quot;);\n        hashTable.put(&quot;key2&quot;, &quot;test2&quot;);\n        hashTable.put(null, &quot;test2&quot;);\n\n        hashTable.forEach((k, v) -&gt; System.out.println(&quot;Key: &quot; + k + &quot; Value: &quot; + v));\n    }\n    \njava.lang.NullPointerException: Cannot invoke &quot;Object.hashCode()&quot; because &quot;key&quot; is null\n\n\tat java.base\/java.util.Hashtable.put(Hashtable.java:481)\n\tat guru.springframework.HashMapvsHashtable.hashTableWithNullKeyTest(HashMapvsHashtable.java:39)\n\tat java.base\/java.lang.reflect.Method.invoke(Method.java:580)\n\tat java.base\/java.util.ArrayList.forEach(ArrayList.java:1596)\n\tat java.base\/java.util.ArrayList.forEach(ArrayList.java:1596)    \" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #24292E\">    @<\/span><span style=\"color: #D73A49\">Test<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #D73A49\">void<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">hashTableWithNullKeyTest<\/span><span style=\"color: #24292E\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        Hashtable&lt;<\/span><span style=\"color: #D73A49\">String<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #D73A49\">String<\/span><span style=\"color: #24292E\">&gt; hashTable <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">new<\/span><span style=\"color: #24292E\"> Hashtable&lt;&gt;();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashTable.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;key1&quot;<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #032F62\">&quot;test&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashTable.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;key2&quot;<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #032F62\">&quot;test2&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashTable.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #005CC5\">null<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #032F62\">&quot;test2&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashTable.<\/span><span style=\"color: #6F42C1\">forEach<\/span><span style=\"color: #24292E\">((k, v) <\/span><span style=\"color: #D73A49\">-&gt;<\/span><span style=\"color: #24292E\"> System.out.<\/span><span style=\"color: #6F42C1\">println<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;Key: &quot;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">+<\/span><span style=\"color: #24292E\"> k <\/span><span style=\"color: #D73A49\">+<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #032F62\">&quot; Value: &quot;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">+<\/span><span style=\"color: #24292E\"> v));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">java.lang.NullPointerException<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> Cannot invoke <\/span><span style=\"color: #032F62\">&quot;Object.hashCode()&quot;<\/span><span style=\"color: #24292E\"> because <\/span><span style=\"color: #032F62\">&quot;key&quot;<\/span><span style=\"color: #24292E\"> is <\/span><span style=\"color: #005CC5\">null<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">\tat java.base<\/span><span style=\"color: #D73A49\">\/<\/span><span style=\"color: #24292E\">java.util.Hashtable.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(Hashtable.java<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #005CC5\">481<\/span><span style=\"color: #24292E\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">\tat guru.springframework.HashMapvsHashtable.<\/span><span style=\"color: #6F42C1\">hashTableWithNullKeyTest<\/span><span style=\"color: #24292E\">(HashMapvsHashtable.java<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #005CC5\">39<\/span><span style=\"color: #24292E\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">\tat java.base<\/span><span style=\"color: #D73A49\">\/<\/span><span style=\"color: #24292E\">java.lang.reflect.Method.<\/span><span style=\"color: #6F42C1\">invoke<\/span><span style=\"color: #24292E\">(Method.java<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #005CC5\">580<\/span><span style=\"color: #24292E\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">\tat java.base<\/span><span style=\"color: #D73A49\">\/<\/span><span style=\"color: #24292E\">java.util.ArrayList.<\/span><span style=\"color: #6F42C1\">forEach<\/span><span style=\"color: #24292E\">(ArrayList.java<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #005CC5\">1596<\/span><span style=\"color: #24292E\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">\tat java.base<\/span><span style=\"color: #D73A49\">\/<\/span><span style=\"color: #24292E\">java.util.ArrayList.<\/span><span style=\"color: #6F42C1\">forEach<\/span><span style=\"color: #24292E\">(ArrayList.java<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #005CC5\">1596<\/span><span style=\"color: #24292E\">)    <\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>The following test tries to set a null value with Hashtable.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#fff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"@Test\n    void hashTableWithNullValueTest() {\n        Hashtable&lt;String, String&gt; hashTable = new Hashtable&lt;&gt;();\n        hashTable.put(&quot;key1&quot;, &quot;test&quot;);\n        hashTable.put(&quot;key2&quot;, &quot;test2&quot;);\n        hashTable.put(&quot;key3&quot;, null);\n\n        hashTable.forEach((k, v) -&gt; System.out.println(&quot;Key: &quot; + k + &quot; Value: &quot; + v));\n    }\n    \njava.lang.NullPointerException\n\tat java.base\/java.util.Hashtable.put(Hashtable.java:476)\n\tat guru.springframework.HashMapvsHashtable.hashTableWithNullValueTest(HashMapvsHashtable.java:49)\n\tat java.base\/java.lang.reflect.Method.invoke(Method.java:580)\n\tat java.base\/java.util.ArrayList.forEach(ArrayList.java:1596)\n\tat java.base\/java.util.ArrayList.forEach(ArrayList.java:1596)\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #24292E\">@<\/span><span style=\"color: #D73A49\">Test<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #D73A49\">void<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">hashTableWithNullValueTest<\/span><span style=\"color: #24292E\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        Hashtable&lt;<\/span><span style=\"color: #D73A49\">String<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #D73A49\">String<\/span><span style=\"color: #24292E\">&gt; hashTable <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">new<\/span><span style=\"color: #24292E\"> Hashtable&lt;&gt;();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashTable.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;key1&quot;<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #032F62\">&quot;test&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashTable.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;key2&quot;<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #032F62\">&quot;test2&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashTable.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;key3&quot;<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #005CC5\">null<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        hashTable.<\/span><span style=\"color: #6F42C1\">forEach<\/span><span style=\"color: #24292E\">((k, v) <\/span><span style=\"color: #D73A49\">-&gt;<\/span><span style=\"color: #24292E\"> System.out.<\/span><span style=\"color: #6F42C1\">println<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;Key: &quot;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">+<\/span><span style=\"color: #24292E\"> k <\/span><span style=\"color: #D73A49\">+<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #032F62\">&quot; Value: &quot;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">+<\/span><span style=\"color: #24292E\"> v));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">java.lang.NullPointerException<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">\tat java.base<\/span><span style=\"color: #D73A49\">\/<\/span><span style=\"color: #24292E\">java.util.Hashtable.<\/span><span style=\"color: #6F42C1\">put<\/span><span style=\"color: #24292E\">(Hashtable.java<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #005CC5\">476<\/span><span style=\"color: #24292E\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">\tat guru.springframework.HashMapvsHashtable.<\/span><span style=\"color: #6F42C1\">hashTableWithNullValueTest<\/span><span style=\"color: #24292E\">(HashMapvsHashtable.java<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #005CC5\">49<\/span><span style=\"color: #24292E\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">\tat java.base<\/span><span style=\"color: #D73A49\">\/<\/span><span style=\"color: #24292E\">java.lang.reflect.Method.<\/span><span style=\"color: #6F42C1\">invoke<\/span><span style=\"color: #24292E\">(Method.java<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #005CC5\">580<\/span><span style=\"color: #24292E\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">\tat java.base<\/span><span style=\"color: #D73A49\">\/<\/span><span style=\"color: #24292E\">java.util.ArrayList.<\/span><span style=\"color: #6F42C1\">forEach<\/span><span style=\"color: #24292E\">(ArrayList.java<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #005CC5\">1596<\/span><span style=\"color: #24292E\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">\tat java.base<\/span><span style=\"color: #D73A49\">\/<\/span><span style=\"color: #24292E\">java.util.ArrayList.<\/span><span style=\"color: #6F42C1\">forEach<\/span><span style=\"color: #24292E\">(ArrayList.java<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #005CC5\">1596<\/span><span style=\"color: #24292E\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Which to Use? HashMap or Hashtable?<\/h2>\n\n\n\n<p>If thread-safety is not a concern, <code>HashMap<\/code> is the clear choice. Its faster and uses less memory than <code>Hashtable<\/code>.<\/p>\n\n\n\n<p>When thread-safety is a concern, you should consider using <code>ConcurrentHashMap<\/code> rather than <code>Hashtable<\/code>.<\/p>\n\n\n\n<p>As of Java 21, <code>Hashtable<\/code> has not been deprecated, but it is considered obsolete by the Java community.  <\/p>\n\n\n\n<p>The source code examples used in this post are available in my <a href=\"https:\/\/github.com\/spring-framework-guru\/spring-6-posts\/tree\/main\/java-examples\" target=\"_blank\" rel=\"noopener\" title=\"\">GitHub repository here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Java, the HashMap and HashTable are both data structures that store data in key value pairs. When you store data in these, the key value is hashed, then is used as an index for fast access to the stored data. HashMap vs Hashtable HashMap Hashtable Introduced in Java 1.2 Introduced in Java 1.0, considered [&hellip;]<a href=\"https:\/\/springframework.guru\/java-hashmap-vs-hashtable\/\" class=\"df-link-excerpt\">Continue reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":4575,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[20],"tags":[27],"class_list":["post-4191","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java"],"jetpack_publicize_connections":[],"aioseo_notices":[],"modified_by":"jt","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/NewBannerBOOTSWeb.jpg","jetpack_shortlink":"https:\/\/wp.me\/p5BZrZ-15B","_links":{"self":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/4191"}],"collection":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/comments?post=4191"}],"version-history":[{"count":9,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/4191\/revisions"}],"predecessor-version":[{"id":8180,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/4191\/revisions\/8180"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media\/4575"}],"wp:attachment":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media?parent=4191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/categories?post=4191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/tags?post=4191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}