This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hash Table

 
Hari Haran
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hari Haran
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Carl I will try that.
Hari
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic