借助Map实现List<Map<String,Object>>一对多处理
1:将下面类型数据格式
[
{
"YZPZXH": 10013719000000038075,
"YZPZMXXH": 2,
"ZSXMMC": "增值税",
"ZSPMMC": "其他行业(17%、16%、13%)",
"JSYJ": 10000000,
"YNSE": 97906.22,
"YZFSRQ": "2019-06-30",
"SKSSQQ": "2019-06-01",
"SKSSQZ": "2019-06-30",
"JKQX": "2019-07-15",
"ZGSWSKFJ_DM": "13702021400",
"YJSKZTMC": "未到期应缴",
"SKCLLXMC": "扣款锁定",
"SKCLLX_DM": "5"
},
{
"YZPZXH": 10013719000000038075,
"YZPZMXXH": 3,
"ZSXMMC": "地方教育附加",
"ZSPMMC": "增值税地方教育附加",
"JSYJ": 979006.22,
"YNSE": 19580.1244,
"YZFSRQ": "2019-06-30",
"SKSSQQ": "2019-06-01",
"SKSSQZ": "2019-06-30",
"JKQX": "2019-07-15",
"ZGSWSKFJ_DM": "13702021400",
"YJSKZTMC": "未到期应缴",
"SKCLLXMC": "扣款锁定",
"SKCLLX_DM": "5"
},
{
"YZPZXH": 10013720000000005844,
"YZPZMXXH": 4,
"ZSXMMC": "城市维护建设税",
"ZSPMMC": "市区(增值税附征)",
"JSYJ": 317.46,
"YNSE": 22.22,
"YZFSRQ": "2020-03-31",
"SKSSQQ": "2020-03-01",
"SKSSQZ": "2020-03-31",
"JKQX": "2020-04-20",
"ZGSWSKFJ_DM": "13702021400",
"YJSKZTMC": "未到期应缴",
"SKCLLXMC": "正常",
"SKCLLX_DM": "0"
}
]
转换为:
[ { "YZPZXH": "10013719000000038075", "DATA": [ { "ZSXMMC": "增值税", "ZGSWSKFJ_DM": "13702021400", "YZPZXH": 10013719000000038075, "YZPZMXXH": 2, "SKCLLXMC": "扣款锁定", "YZFSRQ": "2019-06-30", "YJSKZTMC": "未到期应缴", "SKSSQQ": "2019-06-01", "SKSSQZ": "2019-06-30", "SKCLLX_DM": "5", "YNSE": 97906.22, "JKQX": "2019-07-15", "ZSPMMC": "其他行业(17%、16%、13%)", "JSYJ": 10000000 }, { "ZSXMMC": "地方教育附加", "ZGSWSKFJ_DM": "13702021400", "YZPZXH": 10013719000000038075, "YZPZMXXH": 3, "SKCLLXMC": "扣款锁定", "YZFSRQ": "2019-06-30", "YJSKZTMC": "未到期应缴", "SKSSQQ": "2019-06-01", "SKSSQZ": "2019-06-30", "SKCLLX_DM": "5", "YNSE": 19580.1244, "JKQX": "2019-07-15", "ZSPMMC": "增值税地方教育附加", "JSYJ": 979006.22 } ] }, { "YZPZXH": "10013720000000005844", "DATA": [ { "ZSXMMC": "增值税", "ZGSWSKFJ_DM": "13702021400", "YZPZXH": 10013720000000005844, "YZPZMXXH": 1, "SKCLLXMC": "正常", "YZFSRQ": "2020-03-31", "YJSKZTMC": "未到期应缴", "SKSSQQ": "2020-03-01", "SKSSQZ": "2020-03-31", "SKCLLX_DM": "0", "YNSE": 317.46, "JKQX": "2020-04-20", "ZSPMMC": "建筑物(11%、10%、9%、5%)-增量房", "JSYJ": 10581.9 } ] } ]
2:代码案例
private List<Map<String,Object>> fzcl(List<Map<String,Object>> list){ //以YZPZXH作为KEY放置到Map中 Map<String,List<Map<String,Object>>> resultMap = new HashMap<String,List<Map<String,Object>>>(); for(Map<String,Object> m:list){ String YZPZXH = ""+m.get("YZPZXH"); List<Map<String,Object>> tmpList; if(resultMap.containsKey(YZPZXH)){ tmpList = resultMap.get(YZPZXH); tmpList.add(m); }else{ tmpList = new ArrayList<Map<String,Object>>(); tmpList.add(m); } resultMap.put(YZPZXH, tmpList); } List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>(); //遍历Map组装结果数据 for (Map.Entry<String,List<Map<String,Object>>> entry : resultMap.entrySet()) { Map<String,Object> tmpMap = new HashMap<String,Object>(); tmpMap.put("YZPZXH", entry.getKey()); tmpMap.put("DATA", entry.getValue()); resultList.add(tmpMap); } return resultList; }
r