Java Lambda 表达式 对 Map 对象排序

Map mailParams = new LinkedHashMap<>();

mailParams.put("Action","SingleSendMail");
mailParams.put("AccountName", "[email protected]");
mailParams.put("ReplyToAddress", "true");
mailParams.put("AddressType", "1");
mailParams.put("ToAddress", "[email protected]");
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())));

 

转载于:https://www.cnblogs.com/zhangpengshou/p/6813095.html

你可能感兴趣的:(Java Lambda 表达式 对 Map 对象排序)