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

public static String MapToAsciiString(Map map) {
        String result = "";
        try {
            List> infoIds = new ArrayList<>((Collection>) map.entrySet());
            // 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序)
            Collections.sort(infoIds, new Comparator>() {

                public int compare(Map.Entry o1, Map.Entry o2) {
                    return (o1.getKey()).toString().compareTo(o2.getKey());
                }
            });
            System.out.println(infoIds);
            // 构造签名键值对的格式
            StringBuilder sb = new StringBuilder();
            for (Map.Entry 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;
    }

你可能感兴趣的:(Java)