歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 對<String,Double>類型的Map根據Value數值由大到小排序

對<String,Double>類型的Map根據Value數值由大到小排序

日期:2017/3/1 9:51:23   编辑:Linux編程

在實際中經常用到對<String,Double>類型的Map根據Value數值由大到小排序,可是每次都記不住代碼,需要花好長時間去查找,現在放在這裡,方便以後查找。

List<Map.Entry<String, Double>> wordMap = new ArrayList<Map.Entry<String, Double>>(patternScoresMap.entrySet());
Collections.sort(wordMap, new Comparator<Map.Entry<String, Double>>() {//根據value排序
public int compare(Map.Entry<String, Double> o1,
Map.Entry<String, Double> o2) {
double result = o2.getValue() - o1.getValue();
if(result > 0)
return 1;
else if(result == 0)
return 0;
else
return -1;
}
});

Copyright © Linux教程網 All Rights Reserved