博客
关于我
List<Map>遍历修改map值
阅读量:802 次
发布时间:2023-02-05

本文共 2057 字,大约阅读时间需要 6 分钟。

    

有一个测试方法需要实现:

测试方法test2的实现步骤如下:

步骤一:初始化数据

首先,创建一个用于存储历史数据的列表historyList:

historyList初始化为三个map对象,每个map包含两个键值对:levels和counts:

        List
> historyList = new ArrayList<>(); for(int i = 0; i < 3; i++) { Map
tempMap = new HashMap<>(); tempMap.put("levels", String.valueOf(i+1)); tempMap.put("counts", "0"); historyList.add(tempMap); }

步骤二:创建临时列表

接下来,创建一个临时列表tempList,用于存储需要更新的数据:

        List
> tempList = new ArrayList<>(); for(int i = 0; i < 2; i++) { Map
tempMap = new HashMap<>(); tempMap.put("levels", String.valueOf(i+1)); tempMap.put("counts", i+5); tempList.add(tempMap); }

步骤三:更新历史数据

遍历tempList中的每个map,检查是否有与historyList中相同的levels值:

        if(tempList.size() > 0) {            for (Map
map : historyList) { String key = String.valueOf(map.get("levels")); String value = String.valueOf(map.get("counts")); for (Map
tempmap : tempList) { String tempkey = String.valueOf(tempmap.get("levels")); String tempvalue = String.valueOf(tempmap.get("counts")); if(tempkey.equals(key)) { value = tempvalue; break; } } map.put("counts", value); } }

步骤四:计算总和

最后,计算tempList中所有counts值的总和:

        int upcount = 0;        for (Map
map : tempList) { upcount += Integer.parseInt(String.valueOf(map.get("counts"))); } System.out.println(upcount);

输出结果如下:

        System.out.println("tempList: " + tempList.toString());        System.out.println("historyList: " + historyList.toString());    

转载地址:http://pzufk.baihongyu.com/

你可能感兴趣的文章
Mysql 8.0 新特性
查看>>
MultCloud – 支持数据互传的网盘管理
查看>>
MySQL 8.0.23中复制架构从节点自动故障转移
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>