数组处理,去重&合并相同key的值

背景:

上游返回的 skuIdList 存在相同的Id,skuCount数组与skuID一一对应

处理结果:skuIdList 去重,对应的count总和要加起来,对应的 originalPriceList 和 subtotalPriceList 不变

代码:

Groovy 复制代码
import groovy.json.JsonSlurper
def skuIdList = new JsonSlurper().parseText('["472262851","472302672","472284575","472262851"]');
//logs.add('skuIdList:\n' + skuIdList.toString());
def applySkuList = [];
def originalPriceList = new JsonSlurper().parseText("[0.03,510.0,51.0,0.03]");
def subtotalPriceList = new JsonSlurper().parseText("[0.21,3570.0,408.0,0.21]");
def skuCountList = new JsonSlurper().parseText("[7,7,8,7]");
def parentPackageId = 5008466108;
 
def skuIdCountMap = [:];
def originalPriceList2 =[];
def subtotalPriceList2 =[];
//通过map去重skuID,并重新写对应价格的数组,使其一一对应
for (int i = 0; i < skuIdList.size(); i++) {
    def currentSkuId = skuIdList[i];
    def currentCount = skuCountList[i];
    if(skuIdCountMap.containsKey(currentSkuId)){
        skuIdCountMap[currentSkuId] += currentCount;
    } else {
        skuIdCountMap[currentSkuId] = currentCount; 
        originalPriceList2.add(originalPriceList[i]);
        subtotalPriceList2.add(subtotalPriceList[i]);
    }
}
/*
遍历skuIdCountMap
分别把skuID 与 对应count 写入2个数组
*/
def skuIDs = [];
def skuCounts = [];
for (String key : skuIdCountMap.keySet()) {
   skuCounts.add(skuIdCountMap[key])
    skuIDs.add(key);
}
println skuIdCountMap;
println originalPriceList2;
println subtotalPriceList2;
println skuIDs;
println skuCounts;

运行结果

java 复制代码
[472262851:14, 472302672:7, 472284575:8]
[0.03, 510.0, 51.0]
[0.21, 3570.0, 408.0]
[472262851, 472302672, 472284575]
[14, 7, 8]
相关推荐
键来大师4 小时前
Android15 RK3588 修改默认不锁屏不休眠
android·java·framework·rk3588
合作小小程序员小小店5 小时前
web网页开发,在线%考试管理%系统,基于Idea,vscode,html,css,vue,java,maven,springboot,mysql
java·前端·系统架构·vue·intellij-idea·springboot
多多*6 小时前
maven常用的命令
java·log4j·maven
xie_pin_an6 小时前
MyBatis-Plus 实战:MPJLambdaWrapper 多表联查用法全解析
java·spring boot·spring·mybatis
ᐇ9596 小时前
Java LinkedList集合全面解析:双向链表的艺术与实战
java·开发语言·链表
luyun0202026 小时前
Windows 11操作更丝滑,绝了
java·运维·figma
码银6 小时前
【数据结构】顺序表
java·开发语言·数据结构
Boop_wu6 小时前
[Java EE] 计算机基础
java·服务器·前端
橘子海全栈攻城狮7 小时前
【源码+文档+调试讲解】基于Spring Boot的考务管理系统设计与实现 085
java·spring boot·后端·spring
神仙别闹7 小时前
基于QT(C++) 实现哈夫曼压缩(多线程)
java·c++·qt