{"id":4291,"date":"2017-09-20T10:59:12","date_gmt":"2017-09-20T10:59:12","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=4291"},"modified":"2021-03-21T06:27:56","modified_gmt":"2021-03-21T06:27:56","slug":"difference-between-hashmap-and-hashtable","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/difference-between-hashmap-and-hashtable\/","title":{"rendered":"Difference between HashMap and HashTable?"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>Let&#8217;s look at some of the differences between HashMap and Hashtable in this tutorial. Hashtable is obsolete and we can replace implementations with some derivate of HashMap extensions. Though we talk about the differences in the section both the classes implement the same interfaces <strong>Map, Cloneable, and Serializable.<\/strong><\/p>\n<h3><strong>HashMap<\/strong><\/h3>\n<p>HashMap is not thread-safe and not synchronized. We cannot use HashMap in multi-threaded applications.<\/p>\n<p>Hapmap allows one null key and null values.<\/p>\n<p>HashMap is faster and uses less memory.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.HashMap;\r\nimport java.util.Iterator;\r\nimport java.util.Map;\r\n\r\npublic class HashMapDemo {\r\n  \r\n  public static void main(String[] args) {\r\n    HashMap&lt;String,String&gt; map = new HashMap&lt;String,String&gt;();\r\n    map.put(null,\"Not defined\");\r\n    map.put(\"US\",\"Washington DC\");\r\n    map.put(\"India\",\"New Delhi\");\r\n    map.put(\"France\",\"Paris\");\r\n    map.put(\"XYZ\",null);\r\n    traverse(map);\r\n  }\r\n  \r\n  public static void traverse(HashMap&lt;String,String&gt; map) {\r\n     Iterator itr = map.entrySet().iterator();\r\n        while (itr.hasNext()) {\r\n            Map.Entry kvPair = (Map.Entry)itr.next();\r\n            System.out.println(kvPair.getKey() + \" = \" + kvPair.getValue());\r\n        }\t\t\r\n  }\r\n}\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4307\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/HashMap-Demo-Program.png\" alt=\"HashMap Demo Program\" width=\"1727\" height=\"914\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/HashMap-Demo-Program.png 1727w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/HashMap-Demo-Program-300x159.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/HashMap-Demo-Program-1024x542.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/HashMap-Demo-Program-768x406.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/HashMap-Demo-Program-1536x813.png 1536w\" sizes=\"auto, (max-width: 1727px) 100vw, 1727px\" \/><\/p>\n<h3>HashTable<\/h3>\n<p>Hashtable is thread-safe and is synchronized.<\/p>\n<p>Hashtable does not allow null keys and values.<\/p>\n<p>Hashtable is an obsolete class and not used now. We can replace it with ConcurrentHashMap implementation in multi-threaded environments.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.Hashtable;\r\nimport java.util.Iterator;\r\nimport java.util.Map;\r\n\r\npublic class HashTableDemo {\r\n\r\n  public static void main(String[] args) {\r\n    Hashtable&lt;String,String&gt; table = new Hashtable&lt;String,String&gt;();\r\n    table.put(null,\"Not defined\");\r\n    table.put(\"US\",\"Washington\");\r\n    table.put(\"India\",\"New Delhi\");\r\n    table.put(\"France\",\"Paris\");\r\n  }\r\n  public static void traverse(Hashtable&lt;String,String&gt; table) {\r\n     Iterator itr = table.entrySet().iterator();\r\n        while (itr.hasNext()) {\r\n            Map.Entry kvPair = (Map.Entry)itr.next();\r\n            System.out.println(kvPair.getKey() + \" = \" + kvPair.getValue());\r\n        }\t\t\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-4291\" data-postid=\"4291\" class=\"themify_builder_content themify_builder_content-4291 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Introduction Let&#8217;s look at some of the differences between HashMap and Hashtable in this tutorial. Hashtable is obsolete and we can replace implementations with some derivate of HashMap extensions. Though we talk about the differences in the section both the classes implement the same interfaces Map, Cloneable, and Serializable. HashMap HashMap is not thread-safe and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-4291","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=4291"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4291\/revisions"}],"predecessor-version":[{"id":19730,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4291\/revisions\/19730"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=4291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=4291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=4291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}