File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Hash Table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Hash Table" Watch "Hash Table" New topic
Author

Hash Table

Hari Haran
Greenhorn

Joined: Sep 21, 2000
Posts: 19
Hi All,
I am fetching a list of all the Countries and their keys from the database.
The select query is sorted by country name alphabetically.
But when I use the following code the result is an unsorted list of countries.
Any help is appreciated ASAP.
Following is the code sample:
erdManager e = new erdManager();
Hashtable ht = e.getList("Country_Key");

Enumeration enum1 = ht.elements();
Enumeration enum2 = ht.keys();
String str1;
String str2;
while(enum1.hasMoreElements() && enum2.hasMoreElements())
{
str1 = (String)enum1.nextElement();
str2 = (String)enum2.nextElement();
System.out.println(str1+"-"str2);
}
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
The problem is caused by your use of Hashtable. It sorts objects placed in it by the HashCode of the key Object. Look over the Collections Framework and choice a differnet Collection type which will give you the behavior you want. Possibly TreeSet.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Hari Haran
Greenhorn

Joined: Sep 21, 2000
Posts: 19
Thanx Carl I will try that.
Hari
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hash Table