1. 程式人生 > >把HashMap按照key的自然順序排序

把HashMap按照key的自然順序排序

/**
     * Map按key進行排序
     *
     * @param map
     * @return
     */
    public static Map<String, String> sortMapByKey(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        Map<String, String> sortMap = new TreeMap<>(new
NoticeSignUtil.MapKeyComparator()); sortMap.putAll(map); return sortMap; } /** * 比較器類 * * @Description: * @Author: zhangfengchao * @CreateTime: 2017/4/28 14:57 */ private static class MapKeyComparator implements Comparator<String> { @Override
public int compare(String str1, String str2) { return str1.compareTo(str2); } }