站内搜索: 请输入搜索关键词

当前页面: 开发资料首页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]));


能给个例子吗?


上面的不就是例子么?


我回帖时还没看见你的回帖,谢谢


↑返回目录
前一篇: 给Java web版的朋友们散分~
后一篇: 为什么不能在另外一个页面中获取cookie 的值