I am trying to store integers in a data structure with a String as the key, an example of what im storing is:
key: "Name1", Int: 123
key: "Name2", Int: 124
I want to be able to insert entries so that they are ordered alphabetically for purposes of easy printing to screen.
So far ive been using:
Dictionary<String,Integer> x = new Hashtable<String,Integer>();
x.put("Name1",123);
x.put("Name2",124);
Enumeration<String> e;
String key;
for(e=x.keys(); e.hasMoreElements();){
key=e.nextElement();
System.out.println(key + ": " + x.get(key));
}
This Outputs:
Name2: 124
Name1: 123
Why aren't these in alphabetical order? Is there an alternative to Dictionary that I should know about?