{"id":1,"date":"2018-02-13T11:45:54","date_gmt":"2018-02-13T11:45:54","guid":{"rendered":"https:\/\/www.onlinenote.in\/java\/?p=1"},"modified":"2019-07-13T14:52:18","modified_gmt":"2019-07-13T14:52:18","slug":"sort-map-by-value","status":"publish","type":"post","link":"https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/","title":{"rendered":"Sorting HashMap by values in ascending and descending order"},"content":{"rendered":"<p>The below example shows Java program to sort a Map by values in ascending and descending order, you can see that it\u2019s so easy to sort the Map.<\/p>\n<pre class=\"lang:default decode:true\" title=\"Sort Map by Value\">public static void main(String args[]) {\r\n\r\n Map &lt; String, Integer &gt; map = new HashMap &lt; String, Integer &gt; ();\r\n map.put(\"A\", 34);\r\n map.put(\"B\", 25);\r\n map.put(\"C\", 50);\r\n map.put(\"D\", 50); \/\/ \"duplicate\" value\r\n\r\n System.out.println(entriesSortedByValues(map));\r\n}\r\n\r\nstatic &lt; K, V extends Comparable &lt; ? super V &gt;&gt; List &lt; Entry &lt; K, V &gt;&gt; entriesSortedByValues(Map &lt; K, V &gt; map) {\r\n\r\n List &lt; Entry &lt; K, V &gt;&gt; sortedEntries = new ArrayList &lt; Entry &lt; K, V &gt;&gt; (map.entrySet());\r\n\r\n Collections.sort(sortedEntries, new Comparator &lt; Entry &lt; K, V &gt;&gt; () {\r\n  @Override\r\n  public int compare(Entry &lt; K, V &gt; e1, Entry &lt; K, V &gt; e2) {\r\n   \/\/return e1.getValue().compareTo(e2.getValue()); \/\/ if ascending order\r\n   return e2.getValue().compareTo(e1.getValue()); \/\/ if descending order\r\n  }\r\n });\r\n\r\n return sortedEntries;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The below example shows Java program to sort a Map by values in ascending and descending order, you can see that it\u2019s so easy to sort the Map. public static void main(String args[]) { Map &lt; String, Integer &gt; map = new HashMap &lt; String, Integer &gt; (); map.put(&#8220;A&#8221;, 34); map.put(&#8220;B&#8221;, 25); map.put(&#8220;C&#8221;, 50); map.put(&#8220;D&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.10 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Sorting HashMap by values in ascending and descending order - Java<\/title>\n<meta name=\"description\" content=\"Example shows Java program to sort a Map by values in ascending and descending order, you can see that it\u2019s so easy to sort the Map.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sorting HashMap by values in ascending and descending order - Java\" \/>\n<meta property=\"og:description\" content=\"Example shows Java program to sort a Map by values in ascending and descending order, you can see that it\u2019s so easy to sort the Map.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/\" \/>\n<meta property=\"og:site_name\" content=\"Java\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-13T11:45:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-07-13T14:52:18+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/\",\"url\":\"https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/\",\"name\":\"Sorting HashMap by values in ascending and descending order - Java\",\"isPartOf\":{\"@id\":\"https:\/\/www.onlinenote.in\/java\/#website\"},\"datePublished\":\"2018-02-13T11:45:54+00:00\",\"dateModified\":\"2019-07-13T14:52:18+00:00\",\"author\":{\"@id\":\"\"},\"description\":\"Example shows Java program to sort a Map by values in ascending and descending order, you can see that it\u2019s so easy to sort the Map.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.onlinenote.in\/java\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sorting HashMap by values in ascending and descending order\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.onlinenote.in\/java\/#website\",\"url\":\"https:\/\/www.onlinenote.in\/java\/\",\"name\":\"Java\",\"description\":\"Code Snippets\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.onlinenote.in\/java\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Sorting HashMap by values in ascending and descending order - Java","description":"Example shows Java program to sort a Map by values in ascending and descending order, you can see that it\u2019s so easy to sort the Map.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/","og_locale":"en_US","og_type":"article","og_title":"Sorting HashMap by values in ascending and descending order - Java","og_description":"Example shows Java program to sort a Map by values in ascending and descending order, you can see that it\u2019s so easy to sort the Map.","og_url":"https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/","og_site_name":"Java","article_published_time":"2018-02-13T11:45:54+00:00","article_modified_time":"2019-07-13T14:52:18+00:00","twitter_card":"summary_large_image","twitter_misc":{"Written by":"","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/","url":"https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/","name":"Sorting HashMap by values in ascending and descending order - Java","isPartOf":{"@id":"https:\/\/www.onlinenote.in\/java\/#website"},"datePublished":"2018-02-13T11:45:54+00:00","dateModified":"2019-07-13T14:52:18+00:00","author":{"@id":""},"description":"Example shows Java program to sort a Map by values in ascending and descending order, you can see that it\u2019s so easy to sort the Map.","breadcrumb":{"@id":"https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.onlinenote.in\/java\/sort-map-by-value\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.onlinenote.in\/java\/"},{"@type":"ListItem","position":2,"name":"Sorting HashMap by values in ascending and descending order"}]},{"@type":"WebSite","@id":"https:\/\/www.onlinenote.in\/java\/#website","url":"https:\/\/www.onlinenote.in\/java\/","name":"Java","description":"Code Snippets","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.onlinenote.in\/java\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":""}]}},"_links":{"self":[{"href":"https:\/\/www.onlinenote.in\/java\/wp-json\/wp\/v2\/posts\/1"}],"collection":[{"href":"https:\/\/www.onlinenote.in\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.onlinenote.in\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.onlinenote.in\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.onlinenote.in\/java\/wp-json\/wp\/v2\/comments?post=1"}],"version-history":[{"count":2,"href":"https:\/\/www.onlinenote.in\/java\/wp-json\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":141,"href":"https:\/\/www.onlinenote.in\/java\/wp-json\/wp\/v2\/posts\/1\/revisions\/141"}],"wp:attachment":[{"href":"https:\/\/www.onlinenote.in\/java\/wp-json\/wp\/v2\/media?parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onlinenote.in\/java\/wp-json\/wp\/v2\/categories?post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onlinenote.in\/java\/wp-json\/wp\/v2\/tags?post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}