java中Mapmap按照value降序排列


Map<String,Double>map=new TreeMap<String,Double>();
map.put("mit", 3795104.300);
map.put("ramin", 6.155);
map.put("research", 889.159);
map.put("mix", 1.375);
map.put("gorgeou", 9.341);
map.put("shneiderman", 7.775);

List<Entry<String,Double>>lists=new ArrayList<Entry<String,Double>>(map.entrySet());
Collections.sort(lists,new Comparator<Map.Entry<String, Double>>() {
public int compare(Map.Entry<String, Double> o1,Map.Entry<String, Double> o2)
{
double q1=o1.getValue();
double q2=o2.getValue();
double p=q2-q1;
if(p>0){
return 1;
}
else if(p==0){
return 0;
}
else
return -1;
}
});
for(Map.Entry<String, Double> set:lists){
System.out.println(set.getKey() +" "+set.getValue());
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM