I have a problem with my Hashtable output

Hashtable HashTest = new Hashtable();
HashTest.put("B_1", "A");
HashTest.put("B_3", "B");
HashTest.put("B_5", "C");
HashTest.put("B_7", "D");
HashTest.put("B_9", "E");
HashTest.put("B_11", "F");
HashTest.put("J_1", "G");
HashTest.put("J_3", "H");
HashTest.put("J_5", "I");
HashTest.put("J_7", "J");
HashTest.put("J_9", "K");

Enumeration enum1, enum2;
enum1 = HashTest.keys ();
enum2 = HashTest.elements ();

String CityCode, CityName;
while (enum1.hasMoreElements ())
CityCode = (String) enum1.nextElement ();
CityName = (String) enum2.nextElement ();
System.out.println("City Code " + CityCode + " Name " + CityName );

The out put looks like this:

City Code B_3 Name B
City Code B_1 Name A
City Code B_11 Name F
City Code J_9 Name K
City Code J_7 Name J
City Code J_5 Name I
City Code J_3 Name H
City Code B_9 Name E
City Code J_1 Name G
City Code B_7 Name D
City Code B_5 Name C

This output is not arranged the way I entered it. nay suggestions?
I do not want to sort the output alphabetically, i just want it to display this way
City Code B_1 City Name A
City Code B_3 City Name B
City Code B_5 City Name C
City Code B_7 City Name D
City Code B_9 City Name E
City Code B_11 City Name F
City Code J_1 City Name G
City Code J_3 City Name H
City Code J_5 City Name I
City Code J_7 City Name J
City Code J_9 City Name K

java.util.HashTable and HashMaps are basically unordered collections, even TreeMap will not help, as it sorts all pairs on keys. Using them you wont get correct sequence.

Had this trouble myself couple of months back, and I used Apache's SequenceHashMap from commons-collections.

You need to download jar from Apache's site try using this mirror http://mirrors.crazeekennee.com/apache/commons/collections/binaries/

Change HashTable in your code with SequenceHashMap and you will get the desired results. Also, instead of iterating over enumerations, get the keySet of SequenceHashMap and Iterate over it.

don't use Hashtable, it's to be considered deprecated.
And never call anything "enum" or "Enumeration" to avoid major trouble with modern compilers.

If you want your data ordered, use a LinkedHashMap.

> don't use Hashtable, it's to be considered deprecated.
Plus its synchronized, making it all the more a poor choice if you don't plan on needing thread safe behavior.