您的位置:首页 > 产品设计 > UI/UE

Map根据Value排序

2019-01-21 16:14 113 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/YouthStrive/article/details/86577107
Map根据Value排序
public static Map<String, Double> sortMapByValue(Map<String, Double> oriMap) {
if (oriMap == null || oriMap.isEmpty()) {
return null;
}
Map<String, Double> sortedMap = new LinkedHashMap<String, Double>();
List<Map.Entry<String, Double>> entryList = new ArrayList<Map.Entry<String, Double>>(
oriMap.entrySet());
Collections.sort(entryList, new MapValueComparator());

Iterator<Map.Entry<String, Double>> iter = entryList.iterator();
Map.Entry<String, Double> tmpEntry = null;
while (iter.hasNext()) {
tmpEntry = iter.next();
sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
}
return sortedMap;
}
/*
*排序类
*/
class MapValueComparator implements Comparator<Map.Entry<String, Double>> {
public int compare(Map.Entry<String, Double> me1, Map.Entry<String, Double> me2) {
return me1.getValue().compareTo(me2.getValue());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: