This discussion is archived
4 Replies Latest reply: Jan 10, 2013 6:06 AM by sabre150 RSS

Sorting HashSet without using the tree set?

909741 Newbie
Currently Being Moderated
Hi,

I need to sort the hash set by using the comparator, if i use the comparator in tree set to sort my hash set then it will check the uniqueness condition also using the same comparator. So i will lose some elements in the set.

So i want to sort no need to check the uniqueness.

Is there any API is available in java to achieve this. Like sorting the list we use the Collections.sort method. Like this any API is available to sort the hash set or any other ways can we achieve this (without using tree set sorting the hash set.)?

Thanks in advance.
  • 1. Re: Sorting HashSet without using the tree set?
    sabre150 Expert
    Currently Being Moderated
    You cannot sort a HashSet. Since you have a HashSet the elements must be unique based on the equals() method so unless your TreeSet comparator makes two elements that are equal() not produce a result of zero when compared (and of course two elements that are not equal() must not compare to 0) then you won't lose elements.

    Questions : Why are you not using a TreeSet rather than a HashSet if you need the elements sorted? Whey can't you copy the set elements to an ArrayList and sort that (Isn't there an toList() or toArray() type method on Set? The Javadoc will tell you.)?
  • 2. Re: Sorting HashSet without using the tree set?
    909741 Newbie
    Currently Being Moderated
    Thanks for your reply.

    I had done those things earlier like you said, the application i am using is frequently running out of memory i don't want to use more objects. If i am converting to list then it will create the another object to hold the elements of the hash set. Because of this only i am seeking for any other API is available.

    Edited by: 906738 on Jan 10, 2013 4:34 AM
  • 3. Re: Sorting HashSet without using the tree set?
    gimbal2 Guru
    Currently Being Moderated
    906738 wrote:
    Thanks for your reply.

    I had done those things earlier like you said, the application i am using is frequently running out of memory i don't want to use more objects. If i am converting to list then it will create the another object to hold the elements of the hash set. Because of this only i am seeking for any other API is available.
    Perhaps you should try to find the source of your memory problems in stead of doing your very best to not have to do that; the way you're trying to "solve" things now is only going to lead to epic failure, I mean you're now as far gone as trying to not create objects in an object oriented language.

    Try a profiler.
  • 4. Re: Sorting HashSet without using the tree set?
    sabre150 Expert
    Currently Being Moderated
    >
    Perhaps you should try to find the source of your memory problems in stead of doing your very best to not have to do that
    ++

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points