请问:Map中存放若干整数对象,现要求将其排序后放入List对象中,谢谢!

解决方案 »

  1.   

    treemap 或者遍历 map 实现 自己的 比较规则
      

  2.   


    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);
    }
    }
      

  3.   

    对象是指什么对象,是普通类型的封装对象,还是你构造的类对象?
    如果是你构造的类对象,只能根本你特定的属性进行排序。
    需要实现Comparator<T>接口,实现方法。Collections.sort(list,new Comparator<T>(){
    public int compare(T t1, T t2){
    return 判断处理逻辑;
    }
    };