<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":629,"date":"2021-07-13T23:07:25","date_gmt":"2021-07-13T23:07:25","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=629"},"modified":"2022-05-02T06:18:40","modified_gmt":"2022-05-02T06:18:40","slug":"how-hashmap-works-in-java","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/","title":{"rendered":"How HashMap Works in Java ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"how-hashmap-works-in-java\" class=\"color-pink\" style=\"text-align: justify;\">How HashMap Works in Java ?<\/h2>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-4229\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map.png\" alt=\"\" width=\"1361\" height=\"992\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map.png 1361w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map-300x219.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map-1024x746.png 1024w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map-768x560.png 768w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map-390x284.png 390w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map-820x598.png 820w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map-1180x860.png 1180w\" sizes=\"(max-width: 1361px) 100vw, 1361px\" \/><\/p>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>HashMap is one of the most widly used implementation of Map to store key-value pairs. To implement HashMap with ArrayList.<\/li>\n<li>It is provides two basic HashMap functions they are: get(key) and put(key, value).<\/li>\n<li>While storing code to be checking the duplicate values. While duplicate values are presents to be removed.<\/li>\n<li>Implementation should not be used as a replacement of HashMap. Also while testing the code, make sure that the Object used in the KEY has proper implementation of equals() method.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"text-center row\" style=\"text-align: justify;\"><img decoding=\"async\" class=\"alignnone size-full wp-image-4230\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map-works.png\" alt=\"\" width=\"754\" height=\"650\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map-works.png 754w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map-works-300x259.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map-works-390x336.png 390w\" sizes=\"(max-width: 754px) 100vw, 754px\" \/><\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"example\" class=\"color-blue\">Example:<\/h2>\n<\/div>\n<\/div>\n<div class=\"CodeContent\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<figure class=\"highlight\"><div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">package com.journaldev.util;<br\/><br\/>import java.util.ArrayList;<br\/>import java.util.List;<br\/><br\/>public class MyHashMap {<br\/><br\/>\tclass Container{<br\/>\t\tObject key;<br\/>\t\tObject value;<br\/>\t\tpublic void insert(Object k, Object v){<br\/>\t\t\tthis.key=k;<br\/>\t\t\tthis.value=v;<br\/>\t\t}<br\/>\t}<br\/>\t<br\/>\tprivate Container c;<br\/>\tprivate List&lt;Container&gt; recordList;<br\/>\t<br\/>\tpublic MyHashMap(){<br\/>\t\t<br\/>\t\tthis.recordList=new ArrayList&lt;Container&gt;();<br\/>\t}<br\/>\t<br\/>\tpublic void put(Object k, Object v){<br\/>\t\tthis.c=new Container();<br\/>\t\tc.insert(k, v);<br\/>\t\t\/\/check for the same key before adding<br\/>\t\tfor(int i=0; i&lt;recordList.size(); i++){<br\/>\t\t\tContainer c1=recordList.get(i);<br\/>\t\t\tif(c1.key.equals(k)){<br\/>\t\t\t\t\/\/remove the existing object<br\/>\t\t\t\trecordList.remove(i);<br\/>\t\t\t\tbreak;<br\/>\t\t\t}<br\/>\t\t}<br\/>\t\trecordList.add(c);<br\/>\t}<br\/>\t<br\/>\tpublic Object get(Object k){<br\/>\t\tfor(int i=0; i&lt;this.recordList.size(); i++){<br\/>\t\t\tContainer con = recordList.get(i);<br\/>\t\t\t\/\/System.out.println(&quot;k.toString():&quot;+k.toString()+&quot;con.key.toString()&quot;+con.key.toString());<br\/>\t\t\tif (k.toString()==con.key.toString()) {<br\/>\t\t\t\t<br\/>\t\t\t\treturn con.value;<br\/>\t\t\t}<br\/>\t\t\t<br\/>\t\t}<br\/>\t\treturn null;<br\/>\t}<br\/>\t<br\/>\tpublic static void main(String[] args) {<br\/>\t\tMyHashMap hm = new MyHashMap();<br\/>\t\thm.put(&quot;1&quot;, &quot;1&quot;);<br\/>\t\thm.put(&quot;2&quot;, &quot;2&quot;);<br\/>\t\thm.put(&quot;3&quot;, &quot;3&quot;);<br\/>\t\tSystem.out.println(hm.get(&quot;3&quot;));<br\/>\t\thm.put(&quot;3&quot;, &quot;4&quot;);<br\/>\t\t<br\/>\t\tSystem.out.println(hm.get(&quot;1&quot;));<br\/>\t\tSystem.out.println(hm.get(&quot;3&quot;));<br\/>\t\tSystem.out.println(hm.get(&quot;8&quot;));<br\/>\t}<br\/><br\/>}<\/code><\/pre> <\/div><\/figure>\n<\/div>\n<\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"output\" class=\"color-blue\">Output:<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\"><\/div>\n<\/div>\n<div class=\"Output\">\n<div class=\"hddn\">\n<figure class=\"highlight\" style=\"text-align: justify;\">\n<pre><code class=\"hljs javascript\" data-lang=\"\"><span class=\"nt\"><span class=\"hljs-number\">3<\/span>\r\n<span class=\"hljs-number\">1<\/span>\r\n<span class=\"hljs-number\">4<\/span>\r\n<span class=\"hljs-literal\">null<\/span><\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : HashMap is one of the most widly used implementation of Map to store key-value pairs&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[4004],"tags":[195,491,360,203,199,214,198,363,209,4121,15937,205,2936,222,4118,4111,4120,4122,4113,4112,4115,4119,4123,196,4116,4117,212,213,286,207,366,204,217,282,4023,483,206,200,197,280,364,4114,968,4124],"class_list":["post-629","post","type-post","status-publish","format-standard","hentry","category-java","tag-accenture-interview-questions-and-answers","tag-applied-materials-interview-questions-and-answers","tag-atos-interview-questions-and-answers","tag-capgemini-interview-questions-and-answers","tag-casting-networks-india-pvt-limited-interview-questions-and-answers","tag-cgi-group-inc-interview-questions-and-answers","tag-chetu-interview-questions-and-answers","tag-ciena-corporation-interview-questions-and-answers","tag-collabera-technologies-interview-questions-and-answers","tag-custom-hashmap-implementation-in-java","tag-dark-horse-digital-interview-questions-and-answers","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","tag-fis-global-business-solutions-india-pvt-ltd-interview-questions-and-answers","tag-flipkart-interview-questions-and-answers","tag-hashmap-internal-implementation-analysis-in-java","tag-hashmap-internal-working","tag-hashmap-iterator-implementation","tag-hashmap-java","tag-hashset-vs-hashmap","tag-how-hashmap-works-internally","tag-how-hashmap-works-internally-in-java","tag-how-hashmap-works-internally-in-java-with-example","tag-how-hashtable-works-internally-in-java","tag-ibm-interview-questions-and-answers","tag-implement-hashmap-in-java-interview","tag-implement-hashmap-without-using-collections","tag-indecomm-global-services-interview-questions-and-answers","tag-infosys-technologies-interview-questions-and-answers","tag-lt-infotech-interview-questions-and-answers","tag-mphasis-interview-questions-and-answers","tag-netapp-interview-questions-and-answers","tag-oracle-corporation-interview-questions-and-answers","tag-peoplestrong-interview-questions-and-answers","tag-persistent-systems-interview-questions-and-answers","tag-rbs-india-de-interview-questions-and-answers","tag-reliance-industries-ltd-interview-questions-and-answers","tag-sap-labs-india-pvt-ltd-interview-questions-and-answers","tag-tech-mahindra-interview-questions-and-answers","tag-unitedhealth-group-interview-questions-and-answers","tag-virtusa-consulting-services-pvt-ltd-interview-questions-and-answers","tag-wells-fargo-interview-questions-and-answers","tag-what-is-hashmap","tag-wipro-infotech-interview-questions-and-answers","tag-write-your-own-hashmap-hashtable-implementation-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How HashMap Works in Java ? - Java Interview Questions<\/title>\n<meta name=\"description\" content=\"How HashMap Works in Java ? - HashMap is one of the most widly used implementation of Map to store key-value pairs\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How HashMap Works in Java ? - Java Interview Questions\" \/>\n<meta property=\"og:description\" content=\"How HashMap Works in Java ? - HashMap is one of the most widly used implementation of Map to store key-value pairs\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-13T23:07:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-05-02T06:18:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map.png\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/\",\"name\":\"How HashMap Works in Java ? - Java Interview Questions\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map.png\",\"datePublished\":\"2021-07-13T23:07:25+00:00\",\"dateModified\":\"2022-05-02T06:18:40+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"How HashMap Works in Java ? - HashMap is one of the most widly used implementation of Map to store key-value pairs\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/#primaryimage\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map.png\",\"contentUrl\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map.png\",\"width\":1361,\"height\":992},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How HashMap Works in Java ? - Java Interview Questions","description":"How HashMap Works in Java ? - HashMap is one of the most widly used implementation of Map to store key-value pairs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/","og_locale":"en_US","og_type":"article","og_title":"How HashMap Works in Java ? - Java Interview Questions","og_description":"How HashMap Works in Java ? - HashMap is one of the most widly used implementation of Map to store key-value pairs","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/","og_site_name":"Wikitechy","article_published_time":"2021-07-13T23:07:25+00:00","article_modified_time":"2022-05-02T06:18:40+00:00","og_image":[{"url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map.png"}],"author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/","name":"How HashMap Works in Java ? - Java Interview Questions","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/#primaryimage"},"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map.png","datePublished":"2021-07-13T23:07:25+00:00","dateModified":"2022-05-02T06:18:40+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"How HashMap Works in Java ? - HashMap is one of the most widly used implementation of Map to store key-value pairs","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-hashmap-works-in-java\/#primaryimage","url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map.png","contentUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2021\/07\/hash-map.png","width":1361,"height":992},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=629"}],"version-history":[{"count":4,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/629\/revisions"}],"predecessor-version":[{"id":4231,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/629\/revisions\/4231"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}