• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reg., Hashtable  RSS feed

 
Vasudevan Gopalan
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I created a hastable and it is working fine.I have one small problem.It is displaying from last element.

For e.g I added like this
h.put("a",1)
h.put("b",2)
h.put("c",3)

I used the fillowing code for display.
//displays 3 2 and 1. I want to display as 1,2,3
for(Enumeration e =h.keys();e.hasMoreElements()
{
System.out.println(e.get(e.nextElement())
}
 
WarnerJan Veldhuis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A hashtable makes no gurantees about the order it stores it's keys/values. It doesn't need to, since the values are retreived by key, not by index.
If you want a fixed order List, read about java.util.LinkedList
 
Vasudevan Gopalan
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need both key as well as value for my application in ascending order.I use key as id for one table.I use value for display.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use a Hashtable, then. Use a TreeMap.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!