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

/**
     * Map按key进行排序
     *
     * @param map
     * @return
     */
    public static Map sortMapByKey(Map map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        Map 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);
        }
    }

你可能感兴趣的:(java)