• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashtable

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
How can we order a hashtable? can any one help!
Thanks and Regards,
Anuja K.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm moving this to the Java in General (beginner) forum.
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic