map排序返回字符串

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) + ",");
        }
        System.out.println();
        System.out.println("排序后:");

        // 调用数组的静态排序方法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)+ "&";
            //result += arrayList.get(i);
        }
        System.out.println();
        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());
                    //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;
    }

猜你喜欢

转载自www.cnblogs.com/gjq1126-web/p/12214606.html
今日推荐