Lambda表达式给map排序、求和

1、封装请求数据需要排序进行签名,map如何排序?

  • map数据封装
    public static void main(String[] args) {
        Map<String, String> params = new HashMap<>();
        params.put("stu_no", "123456");
        params.put("stu_name", "颖宝");
        params.put("age", "18");
        params.put("app_id", "1fb665aba37c437280c2c8e5cb1eff3b");
        params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        formatParamMapToString(params, false, false);
        method1(params);
        method2(params);
        method3(params);
        method4(params);
        method5(params);
    }
    
  • 借助list方法、
 public static String formatParamMapToString(Map<String, String> paramMap, boolean urlEncode, boolean keyToLower) {
        // 原有方式排序
        try {
            List<String> paramList = new ArrayList<>();
            for (String key : paramMap.keySet()) {
                paramList.add((keyToLower ? key.toLowerCase() : key) + "="
                        + (urlEncode ? URLEncoder.encode(paramMap.get(key), "utf-8") : paramMap.get(key)));
            }
            Collections.sort(paramList);
            System.out.println("原有借助list排序paramList:" + paramList);

            StringBuilder beforeSign = new StringBuilder();
            for (String param : paramList) {
                beforeSign.append(param);
            }
            return beforeSign.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
  • 方法1、
   private static void method1(Map<String, String> params) {
        // 自己写的
        params = params.entrySet().stream().sorted((t1, t2) -> t1.getKey().compareTo(t2.getKey()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));
        System.out.println("自己写的method1:" + params);
    }
  • 方法2、
  private static void method2(Map<String, String> params) {
        // key 正序
        params = params.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.naturalOrder()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));
        System.out.println("采用函数key正序method2:" + params);
        // LinkedHashMap就是链表+散列表的结构,其底层采用了Linked双向链表来保存节点的访问顺序,所以保证了有序性。此处返回必须是LinkedHashMap,返回HashMap则无序。
    }

  • 方法3、
  private static void method3(Map<String, String> params) {
        // key 倒序
        params = params.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));
        // params.entrySet().forEach(entry -> System.out.println(String.format("%s=%s", entry.getKey(), entry.getValue())));
        System.out.println("采用函数key倒序method3:" + params);
    }
  • 方法4、
  private static void method4(Map<String, String> params) {
        // value 正序
        params = params.entrySet().stream().sorted(Map.Entry.comparingByValue()) // Comparator.naturalOrder()可以不写,默认正序
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));
        System.out.println("采用函数value正序:" + params);

    }
  • 方法5、
 private static void method5(Map<String, String> params) {
        params = params.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));
        System.out.println("采用函数value倒序:" + params);
    }

运行结果
在这里插入图片描述

2、map集合数据求和

 Map<String, String> scoreMap = new HashMap<>();
        scoreMap.put("语文", "100");
        scoreMap.put("数学", "90");
        scoreMap.put("英语", "92");
        IntStream intStream = scoreMap.values().stream().mapToInt(x -> Integer.valueOf(x));
        int sum = intStream.sum();
        System.out.println(sum);
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值