在HashMap中,怎样先按value排序,再按key排序
例如:Map<String,Integer> result = new HashMap<String,Integer>();
先按数字排序,再按字母排序。把HashMap中的key-value 放到TreeMap
中再排序行吗?最好给个例子。

解决方案 »

  1.   

    HashMap是电脑按二进制排的顺序,所以可以说根本没顺序,你要是用它的话也只能用它的key来对应找到它的键
      

  2.   

    实现Comparable 或 Comparator 接口中的,compareTo(Object o) 或 compare(Object o1,Object o2)方法
      

  3.   

    把HashMap中的key-value 放到TreeMap 
    中,再对所得的TreeMap排序行吗?
      

  4.   

    可以换一种方法啊,使用list保存数据
    然后用Collections.sort(list<T>);
      

  5.   

    HashMap自己本身无法放排序的数据,只有把数据取出来然后做个排序算法将排过序的数据放到list或其他class里了
      

  6.   

    直接定义为:Map <String,Integer> result = new LinkedHashMap<String,Integer>(); 就行了
      

  7.   

    先将 Map 中的 key 和 value 全部取出来封装成 JavaBea 数组,再将这个数组排序,
    排序完成后,重新写回 Map 中,写回时采用 LinkedHashMap 可以保证迭代的顺序。下面的代码可以参考一下:import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;public class Test {    public static void main(String[] args) {
            Map<String, Integer> map = new HashMap<String, Integer>();
            map.put("ee", 3);
            map.put("b", 1);
            map.put("d", 2);
            map.put("eee", 3);
            map.put("A", 1);
            map.put("K", 2);
            map.put("ade", 1);
            map.put("c", 2);
            map.put("aee", 3);
            map.put("a", 1);
            map.put("faed", 2);
            map.put("bdd", 1);
            map.put("qec", 2);
            map.put("eade", 3);
            map.put("Aadf", 1);
            map.put("Kqe", 2);        Map<String, Integer> sortMap = new Test().sortMap(map);        for(Map.Entry<String, Integer> entry : sortMap.entrySet()) {
                System.out.println(entry.getKey() + " --> " + entry.getValue());
            }
        }    public <K, V extends Number> Map<String, V> sortMap(Map<String, V> map) {
            class MyMap<M, N> {
                private M key;
                private N value;
                private M getKey() {
                    return key;
                }
                private void setKey(M key) {
                    this.key = key;
                }
                private N getValue() {
                    return value;
                }
                private void setValue(N value) {
                    this.value = value;
                }
            }        List<MyMap<String, V>> list = new ArrayList<MyMap<String, V>>();
            for (Iterator<String> i = map.keySet().iterator(); i.hasNext(); ) {
                MyMap<String, V> my = new MyMap<String, V>();
                String key = i.next();
                my.setKey(key);
                my.setValue(map.get(key));
                list.add(my);
            }        Collections.sort(list, new Comparator<MyMap<String, V>>() {
                public int compare(MyMap<String, V> o1, MyMap<String, V> o2) {
                    if(o1.getValue() == o2.getValue()) {
                        return o1.getKey().compareTo(o2.getKey());
                    }else{
                        return (int)(o1.getValue().doubleValue() - o2.getValue().doubleValue());
                    }
                }
            });        Map<String, V> sortMap = new LinkedHashMap<String, V>();
            for(int i = 0, k = list.size(); i < k; i++) {
                MyMap<String, V> my = list.get(i);
                sortMap.put(my.getKey(), my.getValue());
            }
            return sortMap;
        }
    }
      

  8.   


    不行,TreeMap 是按照 key 来进行排序的,不能按照 value 来排序。
      

  9.   

    能解释一下
     return (int)(o1.getValue().doubleValue() - o2.getValue().doubleValue())
      

  10.   

    return (int)(o1.getValue().doubleValue() - o2.getValue().doubleValue());返回 两个对象间 value 的比较结果,比如,一个是 5,一个是 3,那它们的差就是 2,即 2 > 0 的,
    因此 5 就排在了 3 的后面。如果是小于 0 的,就会排在前面,等于 0 时,我们就去比较 key 的大小。这样的排序规则应该是你想要的,呵呵。我在 12 楼贴的代码有点问题,改正一下:         Collections.sort(list, new Comparator<MyMap<String, V>>() {
                public int compare(MyMap<String, V> o1, MyMap<String, V> o2) {
                    if(o1.getValue().equals(o2.getValue())) {         // 改成这一行
                        return o1.getKey().compareTo(o2.getKey());
                    }else{
                        return (int)(o1.getValue().doubleValue() - o2.getValue().doubleValue());
                    }
                }
            });
      

  11.   

    TreeMap 就行,传一个自己的Comparator ,就可以按照键和值排序了。
      

  12.   


    TreeMap 不行,TreeMap 只能按照 key 来排序!TreeMap 的 API DOC 中带有比较器的构造方法定义:TreeMap(Comparator<? super K> c)很明显,比较器接受的是 key 的比较,而不是值。
      

  13.   

    return (int)(o1.getValue().doubleValue() - o2.getValue().doubleValue()) 
    为什么要doubleValue() 
    return (o1.getValue()-o2.getValue())为什么不行??
    还有火龙果你在12 楼贴的代码可以得到我要得结果,你可以试试。 
      

  14.   

    Map....
    MAP不是一个映射而已嘛....不了解
      

  15.   


    Map <String,Integer> result = new HashMap <String,Integer>();//待排序的HashMap
    Map <String,String> sort = new TreeMap<String,String>();//创建一个TreeMap,TreeMap本身的Key集合就是排序的。
    for(String key:result.keySet()){//迭代HashMap中的元素,构成TreeMap中的元素。
    sort.put(result.get(key).toString()+key, key);//将HashMap中的value值和key值拼接成字符串,构成TreeMap的Key
    }
    //最后,sort中key的先后顺序就是楼主要求的先后顺序,key对应的value值是HashMap中的Key值。
      

  16.   

    HashMap是无序的,如果想排序,必须先把值取出来,有序的貌似是LinkedHashMap呵呵…………楼主老老实实看API吧
      

  17.   

    HashMap的排序需要实现Comparable 或Comparator,来实现key-value排序你可以定义两个HashMap:
    一个装key-value,另一个装value-key,分别排序
      

  18.   

    import java.util.HashMap;
    import java.util.TreeSet;
    import java.util.Set;
    import java.util.Collection;
    public class HashMapSort{
       public static void main(String [] args){
          HashMap<String,Integer> hm = new HashMap<String,Integer>();
          hm.put("a",5);
          hm.put("c",7);
          hm.put("d",8);
          hm.put("v",0);
          hm.put("m",3);
          hm.put("u",10);
          hm.put("e",2);
          hm.put("g",6);      Set<String> set = hm.keySet();//取出所有键
          TreeSet<String> ts =new TreeSet<String>();
          System.out.println("键的原序:");
          for(String s: set){
             
             System.out.println(s);
             ts.add(s);
          }
           System.out.println("按键排序后的顺序:");
          for(String s:ts){
            
             System.out.println(s);
          }
          Collection<Integer> cl= hm.values();//取出所有值
          TreeSet<Integer> ts1 =new TreeSet<Integer>();
          System.out.println("值的原序:");
          for(Integer s: cl){
             
             System.out.println(s);
             ts1.add(s);
          }
          System.out.println("按值排序后的顺序:");
          for(Integer s:ts1){
             
             System.out.println(s);
          }
       }
    }小试了一下,不知符不符合要求!试试吧,应该差不多。只要稍改一下就完全可以了。我想是这样。
      

  19.   

    import java.util.HashMap;
    import java.util.TreeSet;
    import java.util.Set;
    import java.util.Collection;
    import java.util.LinkedHashMap;
    import java.util.Map;
    public class HashMapSort{
    public static void main(String [] args){
    Map <String,Integer> hm= new LinkedHashMap<String,Integer>(); hm.put("a",5);
    hm.put("c",7);
    hm.put("d",8);
    hm.put("v",0);
    hm.put("m",3);
    hm.put("u",10);
    hm.put("e",2);
    hm.put("g",6);
    System.out.println("键的顺序:");
    for(String s: set){System.out.println(s);}
    }
    }