当前页面: 开发资料首页 → JSP 专题 → HashMap的排序问题,希望各位大侠帮忙,在线等,谢谢
HashMap的排序问题,希望各位大侠帮忙,在线等,谢谢
摘要: HashMap的排序问题,希望各位大侠帮忙,在线等,谢谢
关于HashMap的排序问题,按key的升序或降序
Map map = new HashMap();
map.put("1", "John");
map.put("3", "Jane");
map.put("2", "Hacker");
Iterator k = map.keySet().iterator();
while (k.hasNext()) {
String key = (String) k.next();
system.out.println((String)map.get(key));
}
要求输出为:
John
Hacker
Jane
先定义一个类,实现Comparator接口,然后
用Collections.sort(Map map,Comparator comp);
Map map = new HashMap();
map.put("1", "John");
map.put("3", "Jane");
map.put("2", "Hacker");
Object index[] = map.keySet().toArray();
Arrays.sort( index, new Comparator()
{
public int compare(Object src, Object des)
{
Integer first = Integer.valueOf( (String)src );
Integer second = Integer.valueOf( (String)des );
return first.compareTo( second );
}
} );
for (int i = 0; i < index.length; i++)
System.out.println(map.get(index[i]));
能给个例子吗?
上面的不就是例子么?
我回帖时还没看见你的回帖,谢谢