合并List>中的重复数据并对某个字段做累加

时间:2022-08-27 17:51:09

 

package demo1;  

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class mergeData {
public static void main(String[] args) {
List
<Map<String, String>> l = new ArrayList<Map<String, String>>();
Map
<String, String> oneMap = new HashMap<String, String>();
oneMap.put(
"gsId", "8000");
oneMap.put(
"gsName", "总部集中处理中心");
oneMap.put(
"type", "D");
oneMap.put(
"yybcount", "3");
l.add(oneMap);
Map
<String, String> twoMap = new HashMap<String, String>();
twoMap.put(
"gsId", "8008");
twoMap.put(
"gsName", "南京长江路证券营业部");
twoMap.put(
"type", "H");
twoMap.put(
"yybcount", "14");
l.add(twoMap);

List
<Map<String, String>> twoList = new ArrayList<Map<String, String>>();
Map
<String, String> oneMap1 = new HashMap<String, String>();
oneMap1.put(
"gsId", "8008");
oneMap1.put(
"gsName", "南京长江路证券营业部");
oneMap1.put(
"type", "H");
oneMap1.put(
"yybcount", "5");
twoList.add(oneMap1);
Map
<String, String> twoMap2 = new HashMap<String, String>();
twoMap2.put(
"gsId", "8008");
twoMap2.put(
"gsName", "南京止马营证券营业部");
twoMap2.put(
"type", "F");
twoMap2.put(
"yybcount", "9");
twoList.add(twoMap2);

l.addAll(twoList);

List
<Map<String, String>> countList = new ArrayList<Map<String, String>>();
for (int i = 0; i < l.size(); i++) {
String gsId
= l.get(i).get("gsId");
String type
= l.get(i).get("type");
int flag = 0;// 0为新增数据,1为增加count
for (int j = 0; j < countList.size(); j++) {
String gsId_
= countList.get(j).get("gsId");
String type_
= countList.get(j).get("type");

if (gsId.equals(gsId_) && type.equals(type_)) {
int sum = Integer.parseInt(l.get(i).get("yybcount"))
+ Integer.parseInt(countList.get(j).get("yybcount"));
countList.get(j).put(
"yybcount", sum + "");
flag
= 1;
continue;
}
}
if (flag == 0) {
countList.add(l.get(i));
}
}
for (Map<String, String> map : countList) {
System.out.println(map);
}
}
}

 

返回结果:

{gsName=总部集中处理中心, yybcount=3, type=D, gsId=8000}
{gsName=南京长江路证券营业部, yybcount=19, type=H, gsId=8008}
{gsName=南京止马营证券营业部, yybcount=9, type=F, gsId=8008}

 

 

合并数据,记录:

/**
* 人脸识别业务量报表
*
@param map
*
@return
*/
@Override
public String getEchartByYwCout(Map<String, String> map) {
String json
= "";
Map
<String,Object> returnMap = new HashMap<String,Object>();
try {
if (map.isEmpty()) {
List
<Map<String,String>> list = chartsManageMapper.getEchartByYwCout(ParamsUtils.assembleMap(map));
json
= gson.toJson(ParamsUtils.assemReturnMap(returnMap, list,"0"));// mysql数据
} else {
if (null != map.get("type") && !ParamsUtils.validateTime(map).containsKey("error")) {
if ("0".equals(map.get("type"))){
List
<Map<String,String>> list = chartsManageMapper.getEchartByYwCout(ParamsUtils.validateTime(map));
json
= gson.toJson(ParamsUtils.assemReturnMap(returnMap, list,"0")); // mysql数据
}else{
Map
<String, Object> params = new HashMap<String,Object>();
params.put(
"startTime", ParamsUtils.validateTime(map).get("startTime"));
params.put(
"endTime", ParamsUtils.validateTime(map).get("endTime"));
List
<Map<String,String>> mapList = KylinUtil.execQuery(AssemSqlUtils.assemSqlByEchart(params, "yw_kylin"),new String[] {"gsId", "gsName","type","yybcount"});
List
<Map<String,String>> list = ParamsUtils.mergeData(mapList);
json
= gson.toJson(ParamsUtils.assemReturnMap(returnMap, list,"1"));
}
}
}
}
catch (ParseException e) {
e.printStackTrace();
}
return json;
}

 

/**
* 合并数据并做累加
*/
public static List<Map<String, String>> mergeData(List<Map<String, String>> oneList) {
List
<Map<String,String>> list = new ArrayList<Map<String,String>>();
for (int i = 0; i < oneList.size(); i++) {
String gsId
= oneList.get(i).get("gsId");
String type
= oneList.get(i).get("type");
int flag = 0;
for (int j = 0; j < list.size(); j++) {
String gsId_
= list.get(j).get("gsId");
String type_
= list.get(j).get("type");
if (null!=gsId&&null!=gsId_&&!"".equals(gsId)&&!"".equals(gsId_)&&
gsId.equals(gsId_)
&&type.equals(type_)) {
int sum = Integer.parseInt(oneList.get(i).get("yybcount")) + Integer.parseInt(list.get(j).get("yybcount"));
list.get(j).put(
"yybcount", sum + "");
flag
= 1;
continue;
}
}
if (flag == 0) {
list.add(oneList.get(i));
}
}
return list;
}