请问:Map中存放若干整数对象,现要求将其排序后放入List对象中,谢谢! 请问:Map中存放若干整数对象,现要求将其排序后放入List对象中,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 treemap 或者遍历 map 实现 自己的 比较规则 public class TestMapList { public static void main(String[] args){ Map<Integer, Integer> map=new HashMap<Integer, Integer>(); List<Integer>list=new ArrayList<Integer>(); //map中value是整数对象 map.put(1, 3); map.put(2, 5); map.put(3, 12); map.put(4, 4); map.put(5,5); Collection<Integer> c=map.values();//获取map中的value值 Iterator<Integer> it=c.iterator(); while(it.hasNext()){ list.add(it.next());//先将value值加入到list中 } Collections.sort(list);//再对List排序 System.out.println(list); }} 对象是指什么对象,是普通类型的封装对象,还是你构造的类对象?如果是你构造的类对象,只能根本你特定的属性进行排序。需要实现Comparator<T>接口,实现方法。Collections.sort(list,new Comparator<T>(){public int compare(T t1, T t2){return 判断处理逻辑;}}; Java打包问题。回答就有分 Java中怎样用Timer类实现程序在固定时间段运行? 初学大家别见笑啊~~看看我的代码怎么运行if判断不了我输入的条件啊~~ scrollPane 中JTabel怎么更新 急!!! 让java虚拟机启动时覆盖rf.jar包中的类 eclipse visual editor安装成功纪念帖 现在还可以用Vector与Hashtable吗? 新手求助 谁能给我一个最简单的JTree的例子? 有种声音格式AMR后缀的文件,问AMR的Content-Type是什么? JAVA高手的请进 问个csdn里最傻的问题
public class TestMapList {
public static void main(String[] args){
Map<Integer, Integer> map=new HashMap<Integer, Integer>();
List<Integer>list=new ArrayList<Integer>();
//map中value是整数对象
map.put(1, 3);
map.put(2, 5);
map.put(3, 12);
map.put(4, 4);
map.put(5,5);
Collection<Integer> c=map.values();//获取map中的value值
Iterator<Integer> it=c.iterator();
while(it.hasNext()){
list.add(it.next());//先将value值加入到list中
}
Collections.sort(list);//再对List排序
System.out.println(list);
}
}
如果是你构造的类对象,只能根本你特定的属性进行排序。
需要实现Comparator<T>接口,实现方法。Collections.sort(list,new Comparator<T>(){
public int compare(T t1, T t2){
return 判断处理逻辑;
}
};