Map集合Key 按照ASCII码从小到大(字典序)排序
生活随笔
收集整理的這篇文章主要介紹了
Map集合Key 按照ASCII码从小到大(字典序)排序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*** Description:MD5工具生成token* @param value* @return*/
public String getMD5Value(String value){try {MessageDigest messageDigest = MessageDigest.getInstance("MD5");byte[] md5ValueByteArray = messageDigest.digest(value.getBytes());BigInteger bigInteger = new BigInteger(1 , md5ValueByteArray);return bigInteger.toString(16).toUpperCase();} catch (Exception e) {throw new RuntimeException(e);}
}
/*** 生成簽名* @param map* @return*/
public String getSignToken(Map<String, String> map) {String result = "";try {List<Map.Entry<String, String>> infoIds = new ArrayList<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());}});// 構造簽名鍵值對的格式StringBuilder sb = new StringBuilder();for (Map.Entry<String, String> item : infoIds) {if (item.getKey() != null || item.getKey() != "") {String key = item.getKey();String val = item.getValue();if (!(val == "" || val == null)) {sb.append(key + "=" + val + "&");}}}result = sb.toString();//進行MD5加密result = getMD5Value(result);} catch (Exception e) {return null;}return result;
}
總結
以上是生活随笔為你收集整理的Map集合Key 按照ASCII码从小到大(字典序)排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: figma设计_5位来自杂乱无章的设计师
- 下一篇: [html] 怎样避免让用户看到长时间