tag:blogger.com,1999:blog-694855878384792308.post3137998208213593349..comments2024-03-21T06:26:49.387-07:00Comments on Java67: How to Sort HashMap in Java based on Keys and Valuesjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-694855878384792308.post-46361958224935342872022-04-27T21:02:10.084-07:002022-04-27T21:02:10.084-07:00map.entrySet().stream()
.sorted(Map.Entry.compar...map.entrySet().stream()<br /> .sorted(Map.Entry.comparingByValue())<br /> .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,<br /> (oldValue, newValue) -> oldValue, LinkedHashMap::new)); Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-38074359337309140382021-04-27T00:02:07.883-07:002021-04-27T00:02:07.883-07:00This is really coolThis is really coolNiharhttps://www.blogger.com/profile/15574626793072548125noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-33093790004478586502017-12-14T17:18:54.394-08:002017-12-14T17:18:54.394-08:00u use tree map in them.u use tree map in them.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-69535575917994393232017-07-18T08:07:23.713-07:002017-07-18T08:07:23.713-07:00Hello Anonymous, this will not work becuase you ar...Hello Anonymous, this will not work becuase you are collecting result in a Map which may not be ordered and all sorting will be lost. Instead you should use a LinkedHashMap to collect entries e.g. <br />Collections.toMap(LinkedHashMap:new, ....)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-8445390596876879352016-05-05T08:19:26.439-07:002016-05-05T08:19:26.439-07:00If i have custom object that is stored as value in...If i have custom object that is stored as value in map and i want to sort it on the basis of id. What should i use Hashmap or treemap becuase in both the cases i need to implement Comparator in my custom object and override @compare method to provide my own implementation. <br /><br />if answer is treemap then Why because treemap stores the value in natural order and it will not benefit me in any case.Anonymoushttps://www.blogger.com/profile/09030975835526750737noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-32024493723987416572016-03-14T22:20:17.207-07:002016-03-14T22:20:17.207-07:00You can sort the HashMap in Java 8 with less than ...You can sort the HashMap in Java 8 with less than 3 lines of code.<br />map.entrySet().stream()<br /> .sorted(Map.EntrycomparingByValue().reversed())<br /> .collect(Collectors.toMap(e -> e.getKey(), e -> e.value(), (e1,e2) -> e1)); Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-61849308736555990562016-01-16T23:47:34.539-08:002016-01-16T23:47:34.539-08:00Thanks a lot. Explained very well.Thanks a lot. Explained very well.Anonymoushttps://www.blogger.com/profile/08237430737713999466noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-48081418631757393062015-07-24T16:33:37.537-07:002015-07-24T16:33:37.537-07:00Hello @Anonymous, that is to prevent resizing of L...Hello @Anonymous, that is to prevent resizing of LinkedHashMap. You might know that Collection start with default size e.g. 16 and then expand themselves by doubling the size or by a factor when size breaches load factor. If original Map is large than LinkedHashMap will go many round of re-sizing which is expensive as it require copying content from one array to another, to avoid that we have initialized with proper capacity. It's always good practice to initialize Collection with proper capacity, its must when you know the exact size in advance. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-82426330054542820022015-07-24T11:56:05.961-07:002015-07-24T11:56:05.961-07:00Hi,
I have lil doubt regarding below line
Linke...Hi,<br /><br />I have lil doubt regarding below line<br /><br /><br />LinkedHashMap sortedByValue = new LinkedHashMap(listOfEntries.size());<br /><br />Could you please tell, what is need of passing "listOfEntries.size()" while creating LinkedHashMap?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-63728018464669026382015-01-11T04:57:36.310-08:002015-01-11T04:57:36.310-08:00Hi Ike,
HashMap doesn't show any random key o...Hi Ike,<br /> HashMap doesn't show any random key out of duplicates. Actually it can't keep duplicates within it. Whenever there is a duplicate key added to the map it overrides the existing one. So each time there will be only one key available with that name. For your desired output what you have to do is that you have to create another class extending HashMap. Whenever there will will be a entry put to the map you can query if it already exists in that map? if it exists then update the existing value of that key by summession of both the values.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-77840330952645735982015-01-08T04:57:23.518-08:002015-01-08T04:57:23.518-08:00This is a great article. I once had a challenge al...This is a great article. I once had a challenge along these lines that stumped me one time... and till date I still don't know how to do it though I know it can be done somehow. So I'm just going to put it out there incase someone has encountered\solved it before and feels like sharing: <br /><br />Basically, you know how hashmaps & sets don't show duplicates, well, I tried adding the values of duplicate map-keys, so that when displaying a map-key, rather than the hashmap showing one random set(of key to a mapped value for duplicate keys), I wanted to sort it in a way so that whichever duplicate key it displayed, the total value for the duplicate keys is shown in place of the value of the duplicate. I understood just by thinking through what I wanted to do that I had to create a comparator for it. However, I couldn't still create a working comparator for it. <br /><br />For a visual eg of the type of data I'm talking about:<br />eg if the map key values were: map<String, Integer> <br />jon 23<br />Ike 65<br />Linda 75<br />jon 80<br />Helen 32 etc<br />Your output after sorting should have: jon 103Ike Kennedynoreply@blogger.com