{"id":3389,"date":"2019-05-12T15:56:38","date_gmt":"2019-05-12T15:56:38","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3389"},"modified":"2019-05-12T18:12:54","modified_gmt":"2019-05-12T18:12:54","slug":"everything-about-hashmap","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/everything-about-hashmap\/","title":{"rendered":"Everything about HashMap"},"content":{"rendered":"<p><strong>1.How to get elements from HashMap<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static void printMap(Map mp) \r\n{\r\n    Iterator it = mp.entrySet().iterator();\r\n\r\n    while (it.hasNext()) \r\n    {\r\n        Map.Entry pairs = (Map.Entry)it.next();\r\n        System.out.println(pairs.getKey() + &quot; = &quot; + pairs.getValue());\r\n\r\n        \/\/Avoids a ConcurrentModificationException\r\n        it.remove(); \r\n    }\r\n}\r\n<\/pre>\n<p><strong>2.Adding keys to HashMap Finding Next Key<\/strong><br \/>\nTo find the next key while using HashMap with Integer as Key the following function can be used.<\/p>\n<ol>\n<li>Iterate through List of Keys<\/li>\n<li>Sort the Keys<\/li>\n<li>Find the Highest value by looking into Key at size-1<\/li>\n<li>The next key to be used is received by adding 1 to Key(lastMaxElem) at size-1 <\/li>\n<\/ol>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nprivate Integer getNextKey()\r\n{\r\n    List&lt;Integer&gt; keyList = new ArrayList&lt;Integer&gt;();\r\n    int lastMaxElem = 0;\r\n\t\t\r\n    HashMap WaterfallHM = (HashMap) getFromWorkFlowScope(&quot;WaterfallHM&quot;);\t\t\r\n    Set&lt;Integer&gt; keys = WaterfallHM.keySet();\r\n        \r\n    for ( Integer key : keys) {\r\n\tkeyList.add(key);\r\n    }\r\n\t\t\r\n    Collections.sort(keyList); \/\/ Sort the arraylist\r\n    lastMaxElem = keyList.get(keyList.size() - 1);\r\n    lastMaxElem++; \r\n\t\t\r\n    return new Integer(lastMaxElem);\r\n}\r\n<\/pre>\n<p><strong>3.How to Initialize a Constants in HashMap<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Test \r\n{\r\n    private static final Map&lt;Integer, String&gt; MY_MAP = createMap();\r\n\r\n    private static Map&lt;Integer, String&gt; createMap() {\r\n        Map&lt;Integer, String&gt; result = new HashMap&lt;Integer, String&gt;();\r\n        result.put(1, &quot;one&quot;);\r\n        result.put(2, &quot;two&quot;);\r\n        return Collections.unmodifiableMap(result);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>4.Why Map Interface doesnot extend Collections Framework<\/strong><br \/>\nCollection assume elements of one value. Map assumes entries of key\/value pairs. They could have been engineered to re-use the same common interface however some methods they implement are incompatible e.g.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nCollection.remove(Object) - removes an element.\r\nMap.remove(Object) - removes by key, not by entry.\r\n<\/pre>\n<p>There are some methods in common; size(), isEmpty(), clear(), putAll\/addAll()<\/p>\n<p>Collection interface is largely incompatible with the Map interface. If Map extended Collection, what would the add(Object) method do<\/p>\n<p><strong>5.Why need ConcurrentHashMap and CopyOnWriteArrayList<\/strong><br \/>\nhe synchronized collections classes, Hashtable, and Vector, and the synchronized wrapper classes, Collections.synchronizedMap() and Collections.synchronizedList(), provide a basic conditionally thread-safe implementation of Map and List. However, several factors make them unsuitable for use in highly concurrent applications, for example, their single collection-wide lock is an impediment to scalability and it often becomes necessary to lock a collection for a considerable time during iteration to prevent ConcurrentModificationException.ConcurrentHashMap(uses Segments) and CopyOnWriteArrayList implementations provide much higher concurrency while preserving thread safety, with some minor compromises in their promises to callers. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.How to get elements from HashMap public static void printMap(Map mp) { Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry)it.next(); System.out.println(pairs.getKey() + &quot; = &quot; + pairs.getValue()); \/\/Avoids a ConcurrentModificationException it.remove(); } } 2.Adding keys to HashMap Finding Next Key To find the next key while using HashMap with Integer as Key&hellip; <a href=\"https:\/\/codethataint.com\/blog\/everything-about-hashmap\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[157],"tags":[],"class_list":["post-3389","post","type-post","status-publish","format-standard","hentry","category-hashmap"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3389"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3389\/revisions"}],"predecessor-version":[{"id":3394,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3389\/revisions\/3394"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}