{"id":18699,"date":"2015-03-30T14:38:06","date_gmt":"2015-03-30T09:08:06","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=18699"},"modified":"2015-03-30T14:55:45","modified_gmt":"2015-03-30T09:25:45","slug":"multikeymap-nested-maps-solution","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/multikeymap-nested-maps-solution\/","title":{"rendered":"MultikeyMap : Nested maps solution."},"content":{"rendered":"<p>Hi everyone , recently I came accross a usecase where I had to map multiple keys against one value. So Juggling with this I came to know about MultiKeyMap of apache.<\/p>\n<p>MultikeyMap is used to map multiple keys against one value . The purpose of this class is to avoid the need to write code to handle maps of maps. It simply implements java.io.Serializable, java.util.Map and IterableMap.<\/p>\n<p>These provide get(), containsKey(), put() and remove() methods for individual keys which operate without extra object creation.This map is implemented as a decorator of a AbstractHashedMap which enables extra behaviour to be added easily.<\/p>\n<p>[java]<br \/>\nimport org.apache.commons.collections.map.MultiKeyMap<\/p>\n<p>MultiKeyMap.decorate(new LinkedMap()) \/\/creates an ordered map<br \/>\nMultiKeyMap.decorate(new LRUMap()) \/\/creates an least recently used map.<br \/>\nMultiKeyMap.decorate(new ReferenceMap()) \/\/creates a garbage collector sensitive map.<br \/>\n[\/java]<\/p>\n<p>Lets take an example for this:-<\/p>\n<p>[java]<\/p>\n<p>MultiKeyMap multiKeyMap=MultiKeyMap.decorate(new LinkedHahMap())<\/p>\n<p>multiKeyMap.put(&quot;key1&quot;,&quot;key2&quot;,&quot;key3&quot;,&quot;value1&quot;) \/\/ Last parameter is value against defined keys.<br \/>\nmultiKeyMap.put(&quot;key11&quot;,&quot;key22&quot;,&quot;key33&quot;,&quot;key44&quot;,&quot;value11&quot;)<br \/>\nmultiKeyMap.put(&quot;key111&quot;,&quot;key222&quot;,&quot;key333&quot;,&quot;value111&quot;)<br \/>\nmultiKeyMap.put(&quot;key1111&quot;,&quot;key2222&quot;,&quot;value1111&quot;)<\/p>\n<p>[\/java]<\/p>\n<p>This is how we create a map with multiple keys. Now let&#8217;s see how to fetch value out of multiKeyMap for one combination of keys.<\/p>\n<p>PS :- One point to note is you have to provide combination of key in the same sequence that you provided while puting into map.<\/p>\n<p>[java]<br \/>\nString value1 = multiKeyMap.get(&quot;key1&quot;,&quot;key2&quot;,&quot;key3&quot;)<br \/>\nString value2 = multiKeyMap.get(&quot;key11&quot;,&quot;key22&quot;,&quot;key33&quot;,&quot;key44&quot;)<br \/>\n[\/java]<\/p>\n<p>During this learning I came to know about MultiKey which is again an another way to solve above use case.A MultiKey allows multiple map keys to be merged together. Example of MultiKeyMap With MultiKey :-<\/p>\n<p>[java]<br \/>\nimport org.apache.commons.collections.keyvalue.MultiKey<\/p>\n<p>MultiKey multiKey=new MultiKey(&quot;key1&quot;,&quot;key2&quot;,&quot;key3&quot;)<br \/>\nMultikeyMap multiKeyMap=MultiKeyMap.decorator(new LinkedHashMap())<\/p>\n<p>\/\/Map map = new HashMap(); you can also use simple Map while using Multikey.<br \/>\nmultiKeyMap.put(multiKey,&quot;value1&quot;)<\/p>\n<p>\/\/To fetch value from map using multikey, we simply do as follows :-<br \/>\nmultiKeyMap.get(multiKey)<br \/>\n[\/java]<\/p>\n<p>You can also access each key using index of key within MultiKey object. For Example :-<\/p>\n<p>[java]<br \/>\nmultikey.getKey(0) \/\/ output is &#8216;key1&#8217;<br \/>\n[\/java]<\/p>\n<p>I hope this blog was helpful and added useful content to your knowledge. For any queries you can comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi everyone , recently I came accross a usecase where I had to map multiple keys against one value. So Juggling with this I came to know about MultiKeyMap of apache. MultikeyMap is used to map multiple keys against one value . The purpose of this class is to avoid the need to write code [&hellip;]<\/p>\n","protected":false},"author":72,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":54},"categories":[7,446],"tags":[1729],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18699"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/72"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=18699"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18699\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=18699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=18699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=18699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}