{"id":279,"date":"2025-06-24T08:31:24","date_gmt":"2025-06-24T08:31:24","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=279"},"modified":"2025-07-16T10:19:22","modified_gmt":"2025-07-16T10:19:22","slug":"what-is-hashmap","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/what-is-hashmap\/","title":{"rendered":"What is Hashmap?"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">In Java, a <strong>HashMap<\/strong> is a part of the <code>java.util<\/code> package and implements the <code>Map<\/code> interface. It stores data in <strong>key-value pairs<\/strong>, where each key is unique and maps to exactly one value.<\/h4>\n\n\n\n<p>\ud83e\udde0 Key Features of HashMap:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Fast access<\/strong>: Uses a technique called <em>hashing<\/em> to retrieve values quickly based on their keys.<\/li>\n\n\n\n<li><strong>No duplicate keys<\/strong>: If you try to insert a key that already exists, the new value will overwrite the old one.<\/li>\n\n\n\n<li><strong>Allows one <code>null<\/code> key<\/strong> and multiple <code>null<\/code> values.<\/li>\n\n\n\n<li><strong>Not ordered<\/strong>: The order of elements is not guaranteed (use <code>LinkedHashMap<\/code> if you need insertion order).<\/li>\n\n\n\n<li><strong>Not thread-safe<\/strong>: For concurrent access, use <code>ConcurrentHashMap<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>\ud83e\uddea Example:<\/p>\n\n\n\n<p>import java.util.HashMap;<\/p>\n\n\n\n<p>public class Example {<br>public static void main(String[] args) {<br>HashMap scores = new HashMap&lt;&gt;();<br>scores.put(&#8220;Alice&#8221;, 90);<br>scores.put(&#8220;Bob&#8221;, 85);<br>scores.put(&#8220;Charlie&#8221;, 92);<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    System.out.println(scores.get(\"Alice\")); \/\/ Output: 90\n}<\/code><\/pre>\n\n\n\n<p>}Java\u2019s <code>HashMap<\/code> class offers a rich set of methods to manage key-value pairs efficiently. Here\u2019s a handy breakdown of the most commonly used ones:<\/p>\n\n\n\n<p>\ud83d\udd11 Basic Operations<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>put(K key, V value)<\/code><\/strong> \u2013 Adds or updates a key-value pair.<\/li>\n\n\n\n<li><strong><code>get(Object key)<\/code><\/strong> \u2013 Retrieves the value for a given key.<\/li>\n\n\n\n<li><strong><code>remove(Object key)<\/code><\/strong> \u2013 Deletes the entry for the specified key.<\/li>\n\n\n\n<li><strong><code>clear()<\/code><\/strong> \u2013 Removes all entries from the map.<\/li>\n\n\n\n<li><strong><code>size()<\/code><\/strong> \u2013 Returns the number of key-value pairs.<\/li>\n\n\n\n<li><strong><code>isEmpty()<\/code><\/strong> \u2013 Checks if the map is empty.<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udd0d Search &amp; Check<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>containsKey(Object key)<\/code><\/strong> \u2013 Checks if a key exists.<\/li>\n\n\n\n<li><strong><code>containsValue(Object value)<\/code><\/strong> \u2013 Checks if a value exists.<\/li>\n\n\n\n<li><strong><code>getOrDefault(Object key, V defaultValue)<\/code><\/strong> \u2013 Returns the value or a default if the key isn\u2019t found.<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udee0\ufe0f Advanced Utilities<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>putIfAbsent(K key, V value)<\/code><\/strong> \u2013 Adds a key-value pair only if the key isn\u2019t already present.<\/li>\n\n\n\n<li><strong><code>replace(K key, V value)<\/code><\/strong> \u2013 Replaces the value for a key if it exists.<\/li>\n\n\n\n<li><strong><code>replace(K key, V oldValue, V newValue)<\/code><\/strong> \u2013 Replaces only if the current value matches the old one.<\/li>\n\n\n\n<li><strong><code>merge(K key, V value, BiFunction)<\/code><\/strong> \u2013 Combines values for duplicate keys using a function.<\/li>\n\n\n\n<li><strong><code>compute(K key, BiFunction)<\/code><\/strong> \u2013 Recomputes the value for a key.<\/li>\n\n\n\n<li><strong><code>computeIfAbsent(K key, Function)<\/code><\/strong> \u2013 Computes and adds a value if the key is missing.<\/li>\n\n\n\n<li><strong><code>computeIfPresent(K key, BiFunction)<\/code><\/strong> \u2013 Updates the value only if the key exists.<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udd04 Iteration &amp; Views<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>keySet()<\/code><\/strong> \u2013 Returns a <code>Set<\/code> of all keys.<\/li>\n\n\n\n<li><strong><code>values()<\/code><\/strong> \u2013 Returns a <code>Collection<\/code> of all values.<\/li>\n\n\n\n<li><strong><code>entrySet()<\/code><\/strong> \u2013 Returns a <code>Set<\/code> of all key-value pairs.<\/li>\n\n\n\n<li><strong><code>forEach(BiConsumer)<\/code><\/strong> \u2013 Performs an action for each entry.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn what a HashMap is in Java. Discover how it stores key-value pairs, provides fast data access, and plays a vital role in collection-based programming.<\/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-279","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\/279","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=279"}],"version-history":[{"count":2,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/279\/revisions"}],"predecessor-version":[{"id":438,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/279\/revisions\/438"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}