首页 > 其他分享 >数组处理,去重&合并相同key的值

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

时间:2024-04-24 12:55:22浏览次数:22  
标签:key 合并 skuIdCountMap add JsonSlurper 数组 def skuIdList

背景:

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

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

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;

 

标签:key,合并,skuIdCountMap,add,JsonSlurper,数组,def,skuIdList
From: https://www.cnblogs.com/JojoMiss/p/18155034

相关文章

  • postgresql中两张表的聚合函数合并到一列或一行,做除法,并保留两位小数
    --两张表的无关数据合并到一张表SELECTA.name,B.nameFROM(selecto.name,row_number()over(orderbyname)fromtb_orgaso)A FULLJOIN (selectr.name,row_number()over(orderbyr.name)fromtb_regionasr)BONA.row_number=B.row_number;这里是利用......
  • Qt short int 数组大小端转换(qbswap)
    在以下情形中,展示了如何将 shortint 数组从大端序转换为小端序,或者从小端序转换为大端序。1#include<iostream>2#include<cstdint>3intmain()4{5constexprintsize=4;6std::uint16_tarr[size]={0x1234,0x5678,0x9abc,0xdef0};7/......
  • JS基础(二)运算符、流程控制语句、数组对象、JSON对象、Date对象、Math对象、Function对
    一运算符<script>//算数运算符//(1)自加运算varx=10;//x=x+1;//x+=2;varret=x++;//先赋值再计算:x+=1//varret=++x;//先计算再赋值:x+=1console.log(x)......
  • element-ui 合并行或列 table :span-method(行合并)
    element-ui官网案例:table合并行或列 element-ui官网中关于行合并的例子是根据行号进行合并的,这显然不符合我们日常开发需求,因为通常我们table中的数据都是动态生成的,所以需要做一些修改。我们首先解读一下官网实例中的各参数的意义:objectSpanMethod({row,column,rowInde......
  • keycloak~jwks-rsa中使用的设计模式
    com.auth0.jwk组织下面的jwks-rsa包,主要实现的是通过公钥对jwt的签名进行验证,在这个包中,它使用了包装器的设计模式,对默认的jwks提供者添加了一缓存的支持,通过建立一个GuavaCachedJwkProvider类,传入一个默认的UrlJwkProvider来将这个Provider进行包装,让它具有缓存的能力!包装器模式......
  • 手动输入一个数组,并调用函数算出数组之和
    /***********************************************************************************filename:005_数组之和.cauthor:[email protected]:2024/04/18function:手动输入一个数组,并算出数组之和note......
  • 找到一个数组中的最大最小值以及下标
    /***********************************************************************************filename:005_找到一个数组中的最大最小值以及下标.cauthor:[email protected]:2024/04/18function:找到一个数组中的最大......
  • Map使用对象作key
    Map介绍:Map是一个集合,一种依照键(key)存储元素的容器,键(key)很像下标,在List中下标是整数。在Map中键(key)可以是任意类型的对象。Map中不能有重复的键(Key),每个键(key)都有一个对应的值(value);Map的key和value都是泛型的,所以Map的key和value可以任意类型的使用对象做Map的key......
  • keycloak~使用jwks验证token的合法性
    keycloak提供了jwks服务,其地址可以在/auth/realms/fabao/.well-known/openid-configuration的返回结果中找到,jwks_uri它表示了公钥的颁发者,可以使用颁发出来的公钥来验证token的签名,基地址也是固定的/auth/realms/fabao/protocol/openid-connect/certs。springboot构建keycloak的......
  • ASP.NET Core Web API下基于Keycloak的多租户用户授权的实现
    在上文《Keycloak中授权的实现》中,以一个实际案例介绍了Keycloak中用户授权的设置方法。现在回顾一下这个案例:服务供应商(ServiceProvider)发布/WeatherForecastAPI供外部访问在企业应用(Client)里有三个用户:super,daxnet,nobody在企业应用里有两个用户组:administrators,users在企......