• stream.sum List和json互转


    原文链接:https://blog.csdn.net/fighting_xuan/article/details/112609463

    https://blog.csdn.net/weixin_49186526/article/details/116098255

    //这两部分效果相同
    monitorCount = tableNameList.stream().mapToDouble(tableName -> multiSourceMapper.getCountByMonitorTableName((String) tableName, wrapper)).sum();

    for (Object tableName : tableNameList) {
    monitorCount = monitorCount + multiSourceMapper.getCountByMonitorTableName((String) tableName, wrapper);
    }

    数据库中某一个字段需要存入集合类型数据时,最简单的方式将该集合转为json格式存进去。

    // 首先maven引入fastjson jar依赖包

    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.24</version>
    </dependency>
    1
    2
    3
    4
    5
    //代码展示

    List<Map> listMap = new ArrayList<Map>();

    Map map1 = new HashMap();
    map1.put("小明","员工");
    map1.put("小军","主管");
    String jsonString1= JSON.toJSONString(map1);
    System.out.println(jsonString1);

    Map map2 = new HashMap();
    map2.put("小王", "员工");
    map2.put("小红", "主管");

    listMap.add(map1);
    listMap.add(map2);
    String jsonString2= JSON.toJSONString(listMap);
    System.out.println(jsonString2);

    输出:
    jsonString1转化后:{"小明":"员工","小军":"主管"}
    jsonString2转化后:[{"小明":"员工","小军":"主管"},{"小王":"员工","小红":"主管"}]

    json转为List

    代码展示

    String mapList=[{"小明":"员工","小军":"主管"},{"小王":"员工","小红":"主管"}];
    List<Map>mapList=(List<Map>) JSONArray.parse(mapList);

  • 相关阅读:
    2017年5月24日 HTML 基础知识(二)
    2017年5月22日 HTML基础知识(一)
    尼采语录
    Unicode字符串和整数和浮点数
    转义字符
    python第一节
    C# ASP .NET WEB方向和WPF方向,我该如何去选择
    ORA-06550:line 1,column 7;PLS-00201:indentifer '存储过程' must be declared;...PL/SQL Statement ignored 问题
    C# WPF打印报表
    Sql Server 自定义数据类型
  • 原文地址:https://www.cnblogs.com/fswhq/p/16623015.html
Copyright © 2020-2023  润新知