天天看点

(java)根据map中value进行排序

代码如下:

 public static List<Map.Entry<String, Integer>> getSortedHashtableByValue(Map<String,Integer> h) {          

       List<Map.Entry<String,Integer>> l = new ArrayList<Map.Entry<String,Integer>>(h.entrySet());    

       Collections.sort(l, new Comparator<Map.Entry<String, Integer>>() {      

           public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {      

               return (o2.getValue()- o1.getValue());      

           }      

       });       

       return l;    

   }    

List<Map.Entry<String,Integer>> r=getSortedHashtableByValue(m);

for(int i=0;i<m.size();i++){

System.out.println(r.get(i).getKey()+" "+r.get(i).getValue());

}