import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
/**
*
- @author Administrator
*/
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;
}
}
本文介绍了一个Java类,用于实现Map接口的TreeMap按Key值进行排序的功能。通过自定义Comparator比较器,该类能够根据字符串Key的字典顺序对Map进行升序排序。
1428

被折叠的 条评论
为什么被折叠?



