java-Map以value值排序

来源:互联网 发布:如何避免淘宝订单清洗 编辑:程序博客网 时间:2024/06/02 01:30

Map<String,String>map=sortMapByValue(mmap);


public static Map<String, String> sortMapByValue(Map<String, String> oriMap) {

if (oriMap == null || oriMap.isEmpty()) {
return null;
}
Map<String, String> sortedMap = new LinkedHashMap<String, String>();
List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(
oriMap.entrySet());
Collections.sort(entryList, new MapValueComparator());


Iterator<Map.Entry<String, String>> iter = entryList.iterator();
Map.Entry<String, String> tmpEntry = null;
while (iter.hasNext()) {
tmpEntry = iter.next();
sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
}
return sortedMap;
}
public static class MapValueComparator implements Comparator<Map.Entry<String, String>> {
@Override
public int compare(Entry<String, String> o1, Entry<String, String> o2) {
// TODO Auto-generated method stub
return o1.getValue().compareTo(o2.getValue());  
}
}
0 0