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);

posted @ 2022-08-29 08:27  枫树湾河桥  阅读(199)  评论(0编辑  收藏  举报
Live2D