• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How to sort HashSet containing custom Objects...?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
I am in a situation where I need to sort my HashSet. The HashSet contains our custom class objects. Let me explain with the help of example,.

e.g.
I have Employee Class with age and fullName as its instance variable. I am adding the instances of Employee class into my HashSet. Now at one point of time, I need to sort out the the HashSet on the basis of the SurName component of the Employee object. And the end result should be: when i iterate over the HashSet, I should be able to get the Employee object unchanged with their full name, but they are sorted in natural order on the basis of surname.


Please Help

.. paresh
 
Marshal
Posts: 80093
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sets don't have an order. Can you sort a Set at all? That sorting sounds like what you do with a List.
 
Sheriff
Posts: 22817
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Sets don't have an order. Can you sort a Set at all?


How about TreeSet?
But you are right about HashSet - that has no defined order, and LinkedHashSet only has insertion (and optionally access) order, nothing custom.

Paresh, you can use a List like Campbell suggested, then sort it using Collections.sort and a small set of custom java.util.Comparator implementations. Or you can create a temporary TreeSet and use its iterator. For instance:
 
Campbell Ritchie
Marshal
Posts: 80093
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I forgot about TreeSet, Rob.
 
Paresh Wankhede
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmm....
But is there no way of sorting the HashSet as we have in TreeSet?

I have thought of using TreeSet but trying to sort it out using HashSet only.
Please advise...

.. paresh
 
Rob Spoor
Sheriff
Posts: 22817
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As said before, HashSet has an undefined ordering, based on the hash codes but even these are used in a modified form by the HashSet. As such, the ordering is going to be unspecified and there is nothing you can do about that. If you need ordering other than insertion order then TreeSet is the only solution provided by the core API.
 
Campbell Ritchie
Marshal
Posts: 80093
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can always create a TreeSet and a Comparator and copy all the entries across. You may be able to pass one collection to the constructor of the second.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic