Java Lambda 表达式 对 Map 对象排序

Map<String,String> mailParams = new LinkedHashMap<>();

mailParams.put("Action","SingleSendMail");
mailParams.put("AccountName", "service@qq.com");
mailParams.put("ReplyToAddress", "true");
mailParams.put("AddressType", "1");
mailParams.put("ToAddress", "zhangpengshou@qq.com");
mailParams.put("Subject", "Subject");
mailParams.put("HtmlBody","body");
mailParams.put("Format", "json");
mailParams.put("Version", "2015-11-23");
mailParams.put("Signature", "");
mailParams.put("SignatureMethod", "HMAC-SHA1");
mailParams.put("SignatureNonce","");
mailParams.put("SignatureVersion", "1.0");
mailParams.put("AccessKeyId","");
mailParams.put("Timestamp", "2017-05-04T19:55:00Z");

mailParams = mailParams.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.naturalOrder())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));

mailParams.entrySet().forEach(entry -> System.out.println(String.format("%s=%s", entry.getKey(), entry.getValue())));

 

posted on 2017-05-05 15:07  一路前行  阅读(3116)  评论(0编辑  收藏  举报