This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ordering Hashtable elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ordering Hashtable elements" Watch "ordering Hashtable elements" New topic
Author

ordering Hashtable elements

Saritha Reddy
Greenhorn

Joined: Jun 17, 2004
Posts: 8
Hi

I am adding elemets in Hashtabl.I want the elements in the order I add the elements.but Hashtable is

public static void main(String[] args)
{
Hashtable h =new Hashtable();
h.put(" login",new Object());
h.put("Dent",new Object());
h.put(" creditcard",new Object());



Enumeration e= h.keys();
for (; e.hasMoreElements(); ) {
Object oVal= (Object)e.nextElement();
System.out.println("key in HT is "+(String)oVal );
}
}


output is

key in HT is Dent

key in HT is creditcard

key in HT is login


Can soem one help what should I do to guarentee the order I added.Is there any thing else I can use.I want key vaule pairs.I can not use ArrayLsit

Thanks in advance
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Hashtable doesn't maintain the order in which the elements are stored. If you want to preserve the order, you can use java.util.LinkedHashMap.


Mani
Quaerendo Invenietis
Saritha Reddy
Greenhorn

Joined: Jun 17, 2004
Posts: 8
Thank you
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ordering Hashtable elements
 
Similar Threads
criteriaFind
Sorting Vector of Hashtables by a Hashtable value
problem about keys of hashtable
hashtable
better way?