java map value累加_n个List<Map>合并,Map中某属性值相等的value值相加

List tDiets =dietApiService.getDietsByDate(userId, startDate, endDate);

List> list = newArrayList();

List> newList = newArrayList();int talls=0;if(tDiets.size()>0){int a=0;for(TDiet td:tDiets){

talls+=Integer.parseInt(td.getFoodIntakeMeasure());//摄入总量

a++;

String foodIntakeType=td.getFoodIntakeType();

org.json.JSONArray jsonArray= neworg.json.JSONArray(foodIntakeType);int iSize =jsonArray.length();for (int i = 0; i < iSize; i++) {

org.json.JSONObject jsonObj=jsonArray.getJSONObject(i);

String bigtypes= jsonObj.get("bigtypes").toString();

String num= jsonObj.get("num").toString();

Map maMap = new HashMap();

maMap.put("bigtypes", bigtypes);

maMap.put("num", num);if(a>1){//多组数据

newList=list;

Map newMap =new HashMap();int b=0;for (int j = 0; j < list.size(); j++) {

newMap=list.get(j);if(bigtypes.equals(newMap.get("bigtypes").toString())){//key值相等的:value相加

b++;

newList.remove(j);

Map maMap2 = new HashMap();

maMap2.put("bigtypes", bigtypes);int xx= Integer.parseInt(newMap.get("num"))+Integer.parseInt(num);

maMap2.put("num",xx+"");

newList.add(maMap2);break;

}

}if(b==0){//key值与list的map无共同key的:list新增

newList.add(maMap);

}

list=newList;

}else{//只有一组数据

list.add(maMap);

}

}

}//取百分比

newList=newArrayList();for (int c = 0; c < list.size(); c++) {

Map newMap2 =new HashMap();

Map oldmap=new HashMap();

oldmap=list.get(c);

newMap2.put("bigtypes", oldmap.get("bigtypes"));int aa= Integer.parseInt(oldmap.get("num"));

newMap2.put("num",towNumDivide(aa,talls)+"%");

newList.add(newMap2);

}

list=newList;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值