• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sort a HashMap based on a Value object's attribute

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Am trying to sort a Map based on a value object's attribute.
The Map has supplierId(String) as Keys and Supplier object as value. The Map has to be sorted on the Supplier object's name.

But the following code doesn't work!!






Can anyone tell me what's wrong in my code?

Thanks,
Arnav
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The list is sorted, but then you put it in a "sorted" map. However, HashMap is not sorted, and it will never be sorted. There is currently only one map that is sorted, and that is TreeMap.

The problem is that any comparator for a TreeMap needs a reference to the TreeMap to be able to sort it. However, it needs to provide the comparator to the TreeMap constructor, so you can't put a reference to the TreeMap right away.

The following may work (I haven't tested it):
The TreeMapComparator should be a static nested class in your current class; its definition is not needed outside of it, only the fact that it is a Comparator.


Edit: of course you can use a LinkedHashMap; that keeps the insertion order. It won't stay ordered if you put new items though, but I'm not quite sure my above solution will handle that well.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would rather use this form of implementation:




and thats it.

You can also take away the Comparable from the Supplier class and use a Comparator when sorting.
 
Ranch Hand
Posts: 240
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

OUTPUT=

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have seen from the earlier posts that it is impossible to sort a hash map. What you are doing is sort a List and then finding the Map.Entry in the Map with the corresponding “V”. What you are actually doing is sorting a List<String>, which is a completely different task.

By the way: Why are you using a Comparator to sort something which already implements Comparable?
 
Arun Singh Raaj
Ranch Hand
Posts: 240
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya, it is true that Map cannot be sorted by values straight.
And you are right, it could have been done by implementing Comparable interface to the Employee, But  i actually had a case in my mind that if the 'value' of the map object is user-defined type then you could have had Comparator implemented.

By the way, if i had the requirement to sort Map by value then does my way of converting Map values into List then finding it through the EntrySet, slow down the performance?

 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arun Singh Raaj wrote:. . . does my way of converting Map values into List then finding it through the EntrySet, slow down the performance?

What do you think? Look at how many loops you have written.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not have a List<Map.Entrry> with a suitable Comparator?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Such a comparator can easily be created like this:

Or even simpler, when you want to compare the values themselves and not a property of the values:
 
I RELEASE YOU! (for now .... ) Feel free to peruse this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic