{"id":1269,"date":"2022-02-26T07:15:18","date_gmt":"2022-02-26T07:15:18","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1269"},"modified":"2022-02-26T07:15:18","modified_gmt":"2022-02-26T07:15:18","slug":"map-interface","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/map-interface\/","title":{"rendered":"Map Interface"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Java Map interface and its methods.<\/p>\n\n\n\n<p>The&nbsp;<code>Map<\/code>&nbsp;interface of the Java collections framework provides the functionality of the map data structure.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"working\">Working of Map<\/h2>\n\n\n\n<p>In Java, elements of&nbsp;<code>Map<\/code>&nbsp;are stored in&nbsp;<strong>key\/value<\/strong>&nbsp;pairs.&nbsp;<strong>Keys<\/strong>&nbsp;are unique values associated with individual&nbsp;<strong>Values<\/strong>.<\/p>\n\n\n\n<p>A map cannot contain duplicate keys. And, each key is associated with a single value.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/Map.png\" alt=\"Working of the map interface in Java\"\/><\/figure>\n\n\n\n<p>We can access and modify values using the keys associated with them.<\/p>\n\n\n\n<p>In the above diagram, we have values:&nbsp;<var>United States<\/var>,&nbsp;<var>Brazil<\/var>, and&nbsp;<var>Spain<\/var>. And we have corresponding keys:&nbsp;<var>us<\/var>,&nbsp;<var>br<\/var>, and&nbsp;<var>es<\/var>.<\/p>\n\n\n\n<p>Now, we can access those values using their corresponding keys.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;The&nbsp;<code>Map<\/code>&nbsp;interface maintains 3 different sets:<\/p>\n\n\n\n<ul><li>the set of keys<\/li><li>the set of values<\/li><li>the set of key\/value associations (mapping).<\/li><\/ul>\n\n\n\n<p>Hence we can access keys, values, and associations individually.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"classes\">Classes that implement Map<\/h2>\n\n\n\n<p>Since&nbsp;<code>Map<\/code>&nbsp;is an interface, we cannot create objects from it.<\/p>\n\n\n\n<p>In order to use functionalities of the&nbsp;<code>Map<\/code>&nbsp;interface, we can use these classes:<\/p>\n\n\n\n<ul><li>HashMap<\/li><li><a href=\"https:\/\/www.programiz.com\/java-programming\/enummap\">Enu<\/a>m<a href=\"https:\/\/www.programiz.com\/java-programming\/enummap\">Map<\/a><\/li><li><a href=\"https:\/\/www.programiz.com\/java-programming\/linkedhashmap\">LinkedH<\/a>a<a href=\"https:\/\/www.programiz.com\/java-programming\/linkedhashmap\">shMap<\/a><\/li><li>WeakHashMap<\/li><li>TreeMap<\/li><\/ul>\n\n\n\n<p>These classes are defined in the collections framework and implement the&nbsp;<code>Map<\/code>&nbsp;interface.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-map-subclasses.png\" alt=\"HashMap, TreeMap, EnumMap, LinkedHashMap and WeakHashMap classes implements the Java Map interface.\"\/><figcaption>Java Map Subclasses<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"interfaces\">Interfaces that extend Map<\/h2>\n\n\n\n<p>The&nbsp;<code>Map<\/code>&nbsp;interface is also extended by these subinterfaces:<\/p>\n\n\n\n<ul><li>SortedMap<\/li><li>NavigableMap<\/li><li>ConcurrentMap<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-map-subinterfaces.png\" alt=\"SortedMap, NavigableMap and ConcurrentMap extends the Java Map interface\"\/><figcaption>Java Map Subinterfaces<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"use\">How to use Map?<\/h2>\n\n\n\n<p>In Java, we must import the&nbsp;<code>java.util.Map<\/code>&nbsp;package in order to use&nbsp;<code>Map<\/code>. Once we import the package, here&#8217;s how we can create a map.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Map implementation using HashMap\nMap&lt;Key, Value&gt; numbers = new HashMap&lt;&gt;();\n<\/code><\/pre>\n\n\n\n<p>In the above code, we have created a&nbsp;<code>Map<\/code>&nbsp;named&nbsp;<var>numbers<\/var>. We have used the&nbsp;<code>HashMap<\/code>&nbsp;class to implement the&nbsp;<code>Map<\/code>&nbsp;interface.<\/p>\n\n\n\n<p>Here,<\/p>\n\n\n\n<ul><li><var>Key<\/var>&nbsp;&#8211; a unique identifier used to associate each element (value) in a map<\/li><li><var>Value<\/var>&nbsp;&#8211; elements associated by keys in a map<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"methods\">Methods of Map<\/h2>\n\n\n\n<p>The&nbsp;<code>Map<\/code>&nbsp;interface includes all the methods of the&nbsp;<code>Collection<\/code>&nbsp;interface. It is because&nbsp;<code>Collection<\/code>&nbsp;is a super interface of&nbsp;<code>Map<\/code>.<\/p>\n\n\n\n<p>Besides methods available in the&nbsp;<code>Collection<\/code>&nbsp;interface, the&nbsp;<code>Map<\/code>&nbsp;interface also includes the following methods:<\/p>\n\n\n\n<ul><li><strong>put(K, V)<\/strong>&nbsp;&#8211; Inserts the association of a key&nbsp;<var>K<\/var>&nbsp;and a value&nbsp;<var>V<\/var>&nbsp;into the map. If the key is already present, the new value replaces the old value.<\/li><li><strong>putAll()<\/strong>&nbsp;&#8211; Inserts all the entries from the specified map to this map.<\/li><li><strong>putIfAbsent(K, V)<\/strong>&nbsp;&#8211; Inserts the association if the key&nbsp;<var>K<\/var>&nbsp;is not already associated with the value&nbsp;<var>V<\/var>.<\/li><li><strong>get(K)<\/strong>&nbsp;&#8211; Returns the value associated with the specified key&nbsp;<var>K<\/var>. If the key is not found, it returns&nbsp;<code>null<\/code>.<\/li><li><strong>getOrDefault(K, defaultValue)<\/strong>&nbsp;&#8211; Returns the value associated with the specified key&nbsp;<var>K<\/var>. If the key is not found, it returns the&nbsp;<var>defaultValue<\/var>.<\/li><li><strong>containsKey(K)<\/strong>&nbsp;&#8211; Checks if the specified key&nbsp;<var>K<\/var>&nbsp;is present in the map or not.<\/li><li><strong>containsValue(V)<\/strong>&nbsp;&#8211; Checks if the specified value&nbsp;<var>V<\/var>&nbsp;is present in the map or not.<\/li><li><strong>replace(K, V)<\/strong>&nbsp;&#8211; Replace the value of the key&nbsp;<var>K<\/var>&nbsp;with the new specified value&nbsp;<var>V<\/var>.<\/li><li><strong>replace(K, oldValue, newValue)<\/strong>&nbsp;&#8211; Replaces the value of the key&nbsp;<var>K<\/var>&nbsp;with the new value&nbsp;<var>newValue<\/var>&nbsp;only if the key&nbsp;<var>K<\/var>&nbsp;is associated with the value&nbsp;<var>oldValue<\/var>.<\/li><li><strong>remove(K)<\/strong>&nbsp;&#8211; Removes the entry from the map represented by the key&nbsp;<var>K<\/var>.<\/li><li><strong>remove(K, V)<\/strong>&nbsp;&#8211; Removes the entry from the map that has key&nbsp;<var>K<\/var>&nbsp;associated with value&nbsp;<var>V<\/var>.<\/li><li><strong>keySet()<\/strong>&nbsp;&#8211; Returns a set of all the keys present in a map.<\/li><li><strong>values()<\/strong>&nbsp;&#8211; Returns a set of all the values present in a map.<\/li><li><strong>entrySet()<\/strong>&nbsp;&#8211; Returns a set of all the key\/value mapping present in a map.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implementation-classes\">Implementation of the Map Interface<\/h2>\n\n\n\n<p><strong>1. Implementing HashMap Class<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Map;\nimport java.util.HashMap;\n\nclass Main {\n\n    public static void main(String&#91;] args) {\n        \/\/ Creating a map using the HashMap\n        Map&lt;String, Integer&gt; numbers = new HashMap&lt;&gt;();\n\n        \/\/ Insert elements to the map\n        numbers.put(\"One\", 1);\n        numbers.put(\"Two\", 2);\n        System.out.println(\"Map: \" + numbers);\n\n        \/\/ Access keys of the map\n        System.out.println(\"Keys: \" + numbers.keySet());\n\n        \/\/ Access values of the map\n        System.out.println(\"Values: \" + numbers.values());\n\n        \/\/ Access entries of the map\n        System.out.println(\"Entries: \" + numbers.entrySet());\n\n        \/\/ Remove Elements from the map\n        int value = numbers.remove(\"Two\");\n        System.out.println(\"Removed Value: \" + value);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Map: {One=1, Two=2}\nKeys: &#91;One, Two]\nValues: &#91;1, 2]\nEntries: &#91;One=1, Two=2]\nRemoved Value: 2\n<\/samp><\/code><\/pre>\n\n\n\n<p>To learn more about&nbsp;<code>HashMap<\/code>, visit&nbsp;<a href=\"https:\/\/www.programiz.com\/java-programming\/hashmap\">Java HashMap<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>2. Implementing TreeMap Class<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Map;\nimport java.util.TreeMap;\n\nclass Main {\n\n    public static void main(String&#91;] args) {\n        \/\/ Creating Map using TreeMap\n        Map&lt;String, Integer&gt; values = new TreeMap&lt;&gt;();\n\n        \/\/ Insert elements to map\n        values.put(\"Second\", 2);\n        values.put(\"First\", 1);\n        System.out.println(\"Map using TreeMap: \" + values);\n\n        \/\/ Replacing the values\n        values.replace(\"First\", 11);\n        values.replace(\"Second\", 22);\n        System.out.println(\"New Map: \" + values);\n\n        \/\/ Remove elements from the map\n        int removedValue = values.remove(\"First\");\n        System.out.println(\"Removed Value: \" + removedValue);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Map using TreeMap: {First=1, Second=2}\nNew Map: {First=11, Second=22}\nRemoved Value: 11<\/samp><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Java Map interface and its methods. The&nbsp;Map&nbsp;interface of the Java collections framework provides the functionality of the map data structure. Working of Map In Java, elements of&nbsp;Map&nbsp;are stored in&nbsp;key\/value&nbsp;pairs.&nbsp;Keys&nbsp;are unique values associated with individual&nbsp;Values. A map cannot contain duplicate keys. And, each key is associated with a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[398],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1269"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1269"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1269\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}