java中Map map按照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