【发布时间】:2013-06-19 17:18:37
【问题描述】:
我正在尝试将整数存储在以字符串为键的数据结构中, 我存储的一个例子是:
key: "Name1", Int: 123
key: "Name2", Int: 124
我希望能够插入条目,以便它们按字母顺序排列,以便于打印到屏幕上。
到目前为止我一直在使用:
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));
}
这个输出:
Name2: 124
Name1: 123
为什么这些不按字母顺序排列? 有没有我应该知道的字典的替代品?
【问题讨论】:
-
您正在寻找TreeMap
-
Java Ordered Map的可能重复
标签: java sorting data-structures