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 @   枫树湾河桥  阅读(202)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
Live2D
欢迎阅读『stream.sum List和json互转』
点击右上角即可分享
微信分享提示
西雅图
14:14发布
西雅图
14:14发布
6°
多云
东北风
1级
空气质量
相对湿度
91%
今天
中雨
5°/9°
周三
3°/12°
周四
5°/10°