首页 > 代码库 > java hashtable 排序问题

java hashtable 排序问题

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Hashtable<String, String> h = new Hashtable<String, String>();
h.put("6", "666");
h.put("8", "888");
h.put("2", "222");
<br>for (String key : h.keySet())
  System.out.println(key);
 
System.out.println("----------------------------------------------------------");
 
List<String> v = new ArrayList<String>(h.keySet());
Collections.sort(v, new Comparator<Object>() {
  public int compare(Object arg0, Object arg1) {
    return Integer.valueOf(arg0.toString()) - Integer.valueOf(arg1.toString());    // 升序,参数顺序对调时降序<br>  }
});
 
for (String key : v)
  System.out.println(key + " " + h.get(key));

 

hashtable默认以主键降序存储,包括数值和字符串主键,对象类型主要未测试。