当先锋百科网

首页 1 2 3 4 5 6 7

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

public class SortMapByKey implements Comparator{
//按照key值升序排列
public int compare(String str1, String str2) {
return str1.compareTo(str2);
}
public Map<String, Object> sortMap(Map<String, Object> map) {
if (map == null || map.isEmpty()) {
return null;
}
Map<String, Object> sortMap = new TreeMap<String, Object>(new SortMapByKey());
sortMap.putAll(map);
return sortMap;
}
}