{"id":383,"date":"2025-07-01T12:51:14","date_gmt":"2025-07-01T12:51:14","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=383"},"modified":"2025-07-16T09:46:31","modified_gmt":"2025-07-16T09:46:31","slug":"how-does-hashmap-is-implemented-using-key-value-pair","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/how-does-hashmap-is-implemented-using-key-value-pair\/","title":{"rendered":"How does HashMap is implemented using key value pair?"},"content":{"rendered":"\n<p>\ud83e\udde0 Core Concept of a <code>HashMap<\/code><\/p>\n\n\n\n<p>A <code>HashMap<\/code> stores data as a collection of <strong>Entry objects<\/strong>, each consisting of:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <strong>key<\/strong> (used to identify the value)<\/li>\n\n\n\n<li>A <strong>value<\/strong> (the actual data associated with the key)<\/li>\n<\/ul>\n\n\n\n<p>Internally, it uses a <strong>hash table<\/strong>\u2014an array of buckets where entries are stored based on the <strong>hash code of the key<\/strong>.<\/p>\n\n\n\n<p>\ud83e\udde9 How It Works Step by Step<\/p>\n\n\n\n<p>1. <strong>Hashing the Key<\/strong><\/p>\n\n\n\n<p>When you put a key-value pair in the map:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>map.put(\"apple\", 100);<\/code><\/pre>\n\n\n\n<p>Java computes the <strong>hash code<\/strong> of <code>\"apple\"<\/code> using <code>hashCode()<\/code>, then applies a function (often <code>(hash &amp; (n-1))<\/code>) to find the <strong>index<\/strong> in the array.<\/p>\n\n\n\n<p>2. <strong>Bucket Storage<\/strong><\/p>\n\n\n\n<p>The hash code determines which <strong>bucket<\/strong> (slot in the array) the entry goes into.<\/p>\n\n\n\n<p>Each bucket holds a <strong>linked list<\/strong> (or <strong>tree<\/strong> if too many collisions) of entries with the same hash index.<\/p>\n\n\n\n<p>3. <strong>Handling Collisions<\/strong><\/p>\n\n\n\n<p>When two keys map to the same index, Java stores them in the same bucket using a <strong>linked list<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>put(\"cat\", 1);\nput(\"act\", 2); \/\/ Might hash to the same bucket as \"cat\"<\/code><\/pre>\n\n\n\n<p>Java uses <code>equals()<\/code> to differentiate them.<\/p>\n\n\n\n<p>Since Java 8, if a bucket gets too long (more than 8 entries), it converts the bucket to a <strong>balanced tree<\/strong> (like Red-Black Tree) to improve performance.<\/p>\n\n\n\n<p>4. <strong>Retrieving a Value<\/strong><\/p>\n\n\n\n<p>When calling:<\/p>\n\n\n\n<p>map.get(&#8220;apple&#8221;);<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Java computes the hash code \u2192 finds bucket index<\/li>\n\n\n\n<li>Then it <strong>iterates through the bucket<\/strong> to find the correct key using <code>equals()<\/code><\/li>\n\n\n\n<li>Finally, it returns the associated value<\/li>\n<\/ul>\n\n\n\n<p><strong>\ud83e\udde0 Behind-the-Scenes Data Structure<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Bucket0] \u2192 null  \n&#91;Bucket1] \u2192 Entry(\"apple\", 100)  \n&#91;Bucket2] \u2192 Entry(\"banana\", 200) \u2192 Entry(\"berry\", 150)  \n...<\/code><\/pre>\n\n\n\n<p><strong>Each <code>Entry<\/code> has:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Key<\/li>\n\n\n\n<li>Value<\/li>\n\n\n\n<li>Hash<\/li>\n\n\n\n<li>next (pointer to the next entry)<\/li>\n<\/ul>\n\n\n\n<p><strong>\u26a1 Key Takeaways for Interviews<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Topic<\/th><th>Summary<\/th><\/tr><\/thead><tbody><tr><td>Core structure<\/td><td>Array of buckets storing key-value pairs as linked entries<\/td><\/tr><tr><td>Hashing<\/td><td>Uses <code>hashCode()<\/code> + compression function to find index<\/td><\/tr><tr><td>Collision handling<\/td><td>Uses linked list; Java 8+ upgrades to tree if bucket is too long<\/td><\/tr><tr><td>Lookup time<\/td><td>Best: O(1), Worst (pre-Java 8): O(n), Java 8+: O(log n) with trees<\/td><\/tr><tr><td>Key methods<\/td><td><code>put(key, value)<\/code>, <code>get(key)<\/code>, <code>remove(key)<\/code>, <code>containsKey(key)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Discover how HashMap works using key-value pairs. Learn how hashing enables fast access, efficient storage, and collision handling in real-world apps.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[17],"tags":[],"class_list":["post-383","post","type-post","status-publish","format-standard","hentry","category-java-interview-questions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/comments?post=383"}],"version-history":[{"count":2,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/383\/revisions"}],"predecessor-version":[{"id":406,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/383\/revisions\/406"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}