...

View Full Version : hashSet order of elements



BubikolRamios
02-05-2010, 08:10 PM
HashSet hs1 = new HashSet();
hs1.add("plantae");
hs1.add("2");
hs1.add("animalia");
while (it1.hasNext())
{
System.out.println(it1.next());
}

-->


2
animalia
plantae


How to make this work from 'plantae ?

Old Pedant
02-05-2010, 08:40 PM
Don't use a HashSet. By definition, *any* kind of hash table WILL NOT store it's contents in alphabetical order.

If you want an ordered collection, use an ordered collection type. Period.

It's actually somewhat coincidental that you got the values in the order that you did. If you had tried a larger list of values, I'm sure they wouldn't have been that ordered.

Now...if you want "a" to come before "1", then you will have to also generate a custom comparator. But that still shouldn't work with HashSet.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum