Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashtable

 
anuja Edathu
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
How can we order a hashtable? can any one help!
Thanks and Regards,
Anuja K.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to the Java in General (beginner) forum.
 
Sripathi Krishnamurthy
Ranch Hand
Posts: 232
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic