{"id":17,"date":"2020-07-24T10:27:10","date_gmt":"2020-07-24T10:27:10","guid":{"rendered":"https:\/\/system.camp\/index.php\/2020\/07\/24\/sort-hapmap-string-long-in-java\/"},"modified":"2021-11-10T19:41:33","modified_gmt":"2021-11-10T19:41:33","slug":"sort-hapmap-string-long-in-java","status":"publish","type":"post","link":"https:\/\/system.camp\/tutorial\/sort-hapmap-string-long-in-java\/","title":{"rendered":"The simplest way to sort HashMap in JAVA"},"content":{"rendered":"\n

The way JAVA data-structures is it is not always easy to sort HashMaps. So here is a piece of code I frequently use to sort HashMap of type String, Long in JAVA<\/p>\n\n\n\n

\/\/ function to sort hashmap by values  public static HashMap<String, Long> sortByValue(HashMap<String, Long> hm) {\n    \/\/ Create a list from elements of HashMap\n    List<Map.Entry<String, Long>> list =\n        new LinkedList<>(hm.entrySet());\n    \/\/ Sort the list\n    Collections.sort(list, new Comparator<Map.Entry<String, Long>>() {\n      public int compare(Map.Entry<String, Long> o1,\n          Map.Entry<String, Long> o2) {\n        return (o1.getValue()).compareTo(o2.getValue());\n      }\n    });\n    \/\/ put data from sorted list to hashmap\n    HashMap<String, Long> temp = new LinkedHashMap<>();\n    for (Map.Entry<String, Long> aa : list) {\n      temp.put(aa.getKey(), aa.getValue());\n    }\n    return temp;\n}<\/code><\/pre>\n\n\n\n

You can change “Long” to any other datatype like Integer, Float, etc.<\/p>\n","protected":false},"excerpt":{"rendered":"

The way JAVA data-structures is it is not always easy to sort HashMaps. So here is a piece of code I frequently use to sort HashMap of type String, Long in JAVA<\/p>\n","protected":false},"author":1,"featured_media":100,"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},"categories":[40,35],"tags":[10,8,3],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/17"}],"collection":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":5,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":2534,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/17\/revisions\/2534"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/media\/100"}],"wp:attachment":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}