zoukankan      html  css  js  c++  java
  • 参数按照字段名的 ASCII 码排序

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class ASCIISort {
        public static void main(String[] args) {
            //参数
            Map<String,String>map = new HashMap<String,String>();
            map.put("version", "2.0");
            map.put("charset", "UTF-8");
            map.put("sign_type", "RSA_1_256");
            map.put("status", "0");
            map.put("message", "");
            
            // 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序)
            List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(map.entrySet());
            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 + "&");
                    }
                }
            }
            //charset=UTF-8&sign_type=RSA_1_256&status=0&version=2.0&
            System.out.println(sb.toString());
        }
    }
    『愿你我既可以朝九晚五,又能够浪迹天涯』
  • 相关阅读:
    mysql 查询技巧
    如何查看mysql索引
    windows下安装redis以及简单的事例
    Buildroot make网卡interfaces文件被修改
    VirtualBox只能生成32位虚拟机
    python-websocket-server hacking
    crontab定时任务
    Linux修改串口irq
    emmc boot_config文件不存在
    /dev/mem直接操作硬件寄存器
  • 原文地址:https://www.cnblogs.com/zjwwljty/p/9328086.html
Copyright © 2011-2022 走看看