package com.p2p.util; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; /** * 集合排序 * @author shamee-loop * */ public class SortUtils { /** * 按英文首字母排序 * @param arrayToSort */ private static String sortStringArray(List<String> arrayList) { System.out.println("字符型数组排序,排序前:"); String result = ""; for (int i = 0; i < arrayList.size(); i++) { System.out.print(arrayList.get(i) + ","); }// 调用数组的静态排序方法sort,且不区分大小写 // Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER); Collections.sort(arrayList); for (int i = 0; i < arrayList.size(); i++) { System.out.print(arrayList.get(i) + ","); result += arrayList.get(i)+ "&"; } System.out.println("组装接口签名数据:" + result); return result;// 组装接口签名用数据 } /** * 参数封装,获取排序数据 * @param map * @return */ public static String sortMap(Map<String, String> map) { if (map != null && !map.isEmpty()) { List<String> list = new ArrayList<String>(); for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getValue()); if (entry.getValue() != null && !StringUtils.isBlank(entry.getValue().toString()) && !entry.getValue().trim().equals("null")) { list.add(entry.getKey() +"="+ entry.getValue()); } } return sortStringArray(list); } return null; } }
/** * 获取key=value格式字符串 * @param map * @return */ public static String getKeyVAlueSting(Map map) { String sign = ""; Set set = map.keySet(); for (String key : set) { sign += key + "=" + map.get(key) + "&"; } sign = sign.substring(0, sign.length() - 1); return sign; }
/** * 对map进行升序排序并删除空值的key * @param * @return * @Author Yaogh * @Date 2020/3/3 13:52 */ public static String sortDeleteMapReturnUrlParam (Map<String,Object> map) { SortedMap treeMap = new TreeMap<>(map); String params = ""; Set<Map.Entry<String,String>> entrys = treeMap.entrySet(); for (Map.Entry<String ,String> entry : entrys) { if(entry.getValue() != null && !StringUtils.isBlank(entry.getValue().toString()) && !entry.getValue().trim().equals("null")){ params += entry.getKey() + "=" + entry.getValue().toString() + "&"; } } params = params.substring(0,params.length() - 1); treeMap.clear(); treeMap = null; return params; } //按键名升序排列 不删除空值的key public static String sortMapReturnUrlParam(Map<String,Object> map) { SortedMap treeMap = new TreeMap<>(map); String params = ""; Set<Map.Entry<String,String>> entrys = treeMap.entrySet(); for (Map.Entry<String ,String> entry : entrys) { if(entry.getValue() != null && !StringUtils.isBlank(entry.getValue().toString()) && !entry.getValue().trim().equals("null")){ params += entry.getKey() + "=" + entry.getValue().toString() + "&"; } } params = params.substring(0,params.length() - 1); treeMap.clear(); treeMap = null; return params; }