zoukankan      html  css  js  c++  java
  • JAVA8 Lambda 表达式使用心得

    List<HashMap> 指定数据求和:  

       List<HashMap> kk = new ArrayList<>();
            Map mmm = new HashMap<>();
            mmm.put("a", 1);
            mmm.put("b", 4);
            kk.add((HashMap) mmm);
            Map mm = new HashMap<>();
            mm.put("a", 2);
            mm.put("b", 5);
            kk.add((HashMap) mm);
            bind.put("m", kk);

    一        int i = (int) kk.stream().mapToInt((n)->Integer.parseInt(n.get("b").toString())).sum();
               System.out.print(i);

      心得: mapToInt()  字面意思

          n 遍历时 得到的HashMap 对象

          Integer.parseInt(n.get("b").toString())  求和前用于提取需要求和的数

    二    Optional<HashMap> ii = kk.stream().reduce((n1,n2)->{n1.put("a", Integer.parseInt(n1.get("a").toString())+Integer.parseInt(n2.get("a").toString()));return n1;});
            System.out.print(ii.get().get("a"));

      心得:reduce() 用于操作数组的两个对象

           n1,n2  n1 第一次为数组的第一个对象,以后为上一次操作返回的结果,n2 为数组的下一个对象

           {n1.put("a", Integer.parseInt(n1.get("a").toString())+Integer.parseInt(n2.get("a").toString())) ; return n1 ; }  数据处理方式

         ii.get().get("a")  返回结果取值

  • 相关阅读:
    Docker宿主机管理
    Docker常用命令
    Maven专题4——Maven测试
    Spring Boot 2.x 之 Logging
    spark高可用集群搭建立
    elastic插件安装
    单实例安装elastic和启动报错解决
    使用Turbine对集群进行监控
    Centos安装mysql5.6.33
    Centos6安装破解JIRA7.3.8
  • 原文地址:https://www.cnblogs.com/tangzeqi/p/8302019.html
Copyright © 2011-2022 走看看