Originally posted by anuja Karthikeyan:
Hi all,
How can we order a hashtable? can any one help!
Thanks and Regards,
Anuja K.
you cannot. Use a treemap for ordered keys.
If you really want to use Hashtable, do the following way
HashTable ht = new HashTable();
ht.put("key", "value");
//repeat putting the key and value for how much ever elements you want.
Vector v = new Vector(ht.keySet());
Collections.sort(v);
Iterator it = v.iterator();
while (it.hasNext()) {
String str=it.next().toString();
System.out.println(str+" "+ht.get(str).toString());
}
One Disadvantage with this code is there is a vector class which adds to memory. If your application is memory hungry, this can add to your problems.