zoukankan      html  css  js  c++  java
  • 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) + ",");
            }// 调用数组的静态排序方法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;
        }
    
    
  • 相关阅读:
    php 5.3新特性
    php:// — 访问各个输入/输出流(I/O streams)
    php 二维数组排序
    js 面向对象式编程
    jQuery 源码学习笔记
    c++ 指针(二)
    c++ 指针(一)
    visual studio 2012 的制作ActiveX、打包和发布
    用linqPad帮助你快速学习LINQ
    Caliburn.Micro学习笔记(五)----协同IResult
  • 原文地址:https://www.cnblogs.com/gjq1126-web/p/12214606.html
Copyright © 2011-2022 走看看