1.下边是一个map形式的key,values转换成指定字符串,
TreeMap中所有的元素都保持着某种固定的顺序,如果你需要得到一个有序的结果你就应该使用TreeMap
Map<String, Object>map = new TreeMap<>();
map.put("name","黄家驹");
map.put("age","18");
map.put("sex","男");
2.这是一个工具类参数为map
public class EncryptHelper {
public static String getUrlParamsByMap(Map<String, Object> map) {
if (map == null) {
return "";
}
StringBuffer sb = new StringBuffer();
for (Map.Entry<String, Object> entry : map.entrySet()) {
sb.append(entry.getKey() + "=" + entry.getValue());
sb.append("&");
}
return sb.toString();
}
}
3.调用后返回结果
String urlParamsByMap = EncryptHelper.getUrlParamsByMap(map);
输出; age=18&name=黄家驹&sex=男&
4.如果想截取后边的&如下;
String substring = urlParamsByMap.substring(0, urlParamsByMap.length() - 1);
输出; age=18&name=黄家驹&sex=男
TreeMap转换字符串
最新推荐文章于 2024-01-23 16:25:14 发布