views:

227

answers:

3

need an example on how to use a comparable class on a hashset to get an ascending order? Let say we have a hashset like: HashSet hs=new HashSet(); How can i get the hs to be in a ascending order??

+3  A: 

HashSet "makes no guarantees as to the iteration order of the set." Use LinkedHashSet instead.

Addendum: I would second @BalusC's point about implementing Comparable and express a slight preference for LinkedHashSet, which offers "predictable iteration order ... without incurring the increased cost associated with TreeSet."

Addendum: @Stephen raises an important point, which favors @BalusC's suggestion of TreeMap. LinkedHashSet is a more efficient alternative only if the data is (nearly) static and already sorted.

trashgod
AFAIK, a linked hashset preserves the (temporal) insertion order. It is hard (i.e. inefficient) to use a linked hashset to implement a sorted set in the face of random insertions. You would need to rebuild the entire LinkedHashSet each time you did a random insertion.
Stephen C
@Stephen C: I believe you are correct; I've elaborated above. I'd welcome any clarification.
trashgod
+1  A: 

HashSets do not guarantee iteration order:

This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

You probably need to choose a different datastructure if you want to be able to control the iteration order (or indeed have one at all!)

Gian
can i make this as a collection set and then use the comparator??http://download.oracle.com/javase/6/docs/api/java/util/Comparator.html
Jonathan
AFAIK, a linked hashset preserves the (temporal) insertion order. It is hard (i.e. inefficient) to use a linked hashset to implement a sorted set in the face of random insertions.
Stephen C
@Stephen C: Good point, I hadn't looked very closely, which is why I just linked it under the suggestion of "a different data structure", because the name sounded approximately correct.
Gian
+8  A: 

Use a TreeSet instead. It has a constructor taking a Comparator. It will automatically sort the Set.

If you want to convert a HashSet to a TreeSet, then do so:

Set<YourObject> hashSet = getItSomehow();
Set<YourObject> treeSet = new TreeSet<YourObject>(new YourComparator());
treeSet.addAll(hashSet);
// Now it's sorted based on the logic as implemented in YourComparator.

If the items you have itself already implements Comparable and its default ordering order is already what you want, then you basically don't need to supply a Comparator. You could then construct the TreeSet directly based on the HashSet. E.g.

Set<String> hashSet = getItSomehow();
Set<String> treeSet = new TreeSet<String>(hashSet);
// Now it's sorted based on the logic as implemented in String#compareTo().

See also:

BalusC
Thanks worked for me ..i didn't need the comparator.
Jonathan