Map集合按照ASCII码从小到大(字典序)排序--JAVA

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28112129/article/details/81455922
public static String MapToAsciiString(Map<String, Object> map) {
        String result = "";
        try {
            List<Map.Entry<String, String>> infoIds = new ArrayList<>((Collection<? extends Map.Entry<String,          String>>) map.entrySet());
            // 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序)
            Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {

                public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                    return (o1.getKey()).toString().compareTo(o2.getKey());
                }
            });
            System.out.println(infoIds);
            // 构造签名键值对的格式
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> item : infoIds) {
                if (item.getKey() != null || item.getKey() != "") {
//                    String key = item.getKey();
                    Object val = item.getValue();
                    if (!(val == "" || val == null)) {
//                        sb.append(key + ":" + val + ":");
                        sb.append(val);
                    }
                }

            }
            result = sb.toString();
        } catch (Exception e) {
            return null;
        }
        return result;
    }

猜你喜欢

转载自blog.csdn.net/qq_28112129/article/details/81455922