博客
关于我
List<Map>遍历修改map值
阅读量:792 次
发布时间: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/

你可能感兴趣的文章
liunx上安装nodejs步骤
查看>>
Liunx中各种压缩包及解压命令
查看>>
liunx命令查看cpu使用率和负载情况
查看>>
liunx快速修改文件夹或文件的属性
查看>>
Liunx挂载nfts盘数据方法
查看>>
liunx查找当前目录文件及子目录文件下的中文并替换
查看>>
liunx环境下的mysql数据库配置文件my.conf内的参数含义
查看>>
liunx目录和文件管理(一)
查看>>
liunx系统中的文件压缩与解压
查看>>
liunx编写启动,kill进程脚本
查看>>
liux的学习笔记
查看>>
live555 testrtspclient客户端建立rtp over tcp 异常问题
查看>>
LiveBOS UploadFile.do 任意文件上传漏洞复现(XVE-2023-21708)
查看>>
LiveData Call Adapter for Retrofit 使用教程
查看>>
LiveData的分析与简单使用
查看>>
LiveGBS user/save 逻辑缺陷漏洞复现(CNVD-2023-72138)
查看>>
live和on的区别
查看>>
Liya Linux:Arch 的又一尝试,提供 Cinnamon 和 MATE 桌面,底层为 Btrfs
查看>>
li下的ul----多级列表
查看>>
lk部分没有msm8937相关目录原因(指向msm8952)
查看>>