Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "hashtable" Watch "hashtable" New topic
Author

hashtable

anuja Edathu
Ranch Hand

Joined: Sep 09, 2005
Posts: 84
Hi all,
How can we order a hashtable? can any one help!
Thanks and Regards,
Anuja K.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I'm moving this to the Java in General (beginner) forum.
Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
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.
 
jQuery in Action, 2nd edition
 
subject: hashtable