程序代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestMapSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<String,Integer> map_Data=new HashMap<String,Integer>();
map_Data.put("A", 98);
map_Data.put("B", 50);
map_Data.put("C", 50);
map_Data.put("D", 25);
map_Data.put("E", 85);
System.out.println(map_Data);
List<Map.Entry<String, Integer>> list_Data = new ArrayList<Map.Entry<String, Integer>>(map_Data.entrySet());
Collections.sort(list_Data, new Comparator<Map.Entry<String, Integer>>()
{
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2)
{
if(o2.getValue()!=null&&o1.getValue()!=null&&o2.getValue().compareTo(o1.getValue())>0){
return 1;
}else{
return -1;
}
}
});
System.out.println(list_Data);
}
}
运行结果:
{D=25, E=85, A=98, B=50, C=50}
[A=98, E=85, C=50, B=50, D=25]