对一组String字符串,使用谷歌的Gson转换为Map<String, List<IndicatorPageDaoBO>,在对数据进行分组统计数量操作。
实体类:
public class IndicatorPageDaoBO {
private Long indicatorIdentifierId;
private Long indicatorValueId;
private String periodId;
private String institutionCode;
private String productCode;
private String nodeCode;
private BigDecimal value;
private BigDecimal amount;
}
主方法:
import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.IndicatorPageDaoBO;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class aaaaaa {
public static void main(String[] args) {
String zhi="{\"125629597553664-SDXZRB0005\":[{\"periodId\":\"8ac275c57fb20c99017fd49c994c0050\",\"amount\":null,\"productCode\":\"SDXZRB0005\"," +
"\"institutionCode\":\"125629597553664\",\"nodeCode\":\"ZBDECJ002\",\"indicatorIdentifierId\":165761,\"indicatorValueId\":91929,\"value\":0}]," +
"\"178040247342840-SDXZRB0002\":[{\"periodId\":\"8ac275c57fb20c99017fd49c994c0050\",\"amount\":null,\"productCode\":\"SDXZRB0002\"," +
"\"institutionCode\":\"178040247342840\",\"nodeCode\":\"BJ001test\",\"indicatorIdentifierId\":165573,\"indicatorValueId\":90923,\"value\":0}]," +
"\"B00002-SDXZRB0005\":[{\"periodId\":\"8ac275c57fb20c99017fd49c994c0050\",\"amount\":null,\"productCode\":\"SDXZRB0005\"," +
"\"institutionCode\":\"B00002\",\"nodeCode\":\"1234561\",\"indicatorIdentifierId\":165571,\"indicatorValueId\":90921,\"value\":0}]," +
"\"178040247342840-SDXZRB0001\":[{\"periodId\":\"8ac275c57fb20c99017fd49c994c0050\",\"amount\":null,\"productCode\":\"SDXZRB0001\"," +
"\"institutionCode\":\"178040247342840\",\"nodeCode\":\"SHZJ001\",\"indicatorIdentifierId\":165575,\"indicatorValueId\":89421,\"value\":0}]," +
"\"ZJTY001-SDXZRB0005\":[{\"periodId\":\"8ac275c57fb20c99017fd49c994c0050\",\"amount\":null,\"productCode\":\"SDXZRB0005\"," +
"\"institutionCode\":\"ZJTY001\",\"nodeCode\":\"ZBDECJ002\",\"indicatorIdentifierId\":165557,\"indicatorValueId\":89423,\"value\":0}]," +
"\"ZZGC001-SDXZRB0005\":[{\"periodId\":\"8ac275c57fb20c99017fd49c994c0050\",\"amount\":null,\"productCode\":\"SDXZRB0005\"," +
"\"institutionCode\":\"ZZGC001\",\"nodeCode\":\"SHZJ001\",\"indicatorIdentifierId\":165521,\"indicatorValueId\":89425,\"value\":0}," +
"{\"periodId\":\"8ac275c57fb20c99017fd49c994c0050\",\"amount\":null,\"productCode\":\"SDXZRB0005\",\"institutionCode\":\"ZZGC001\"," +
"\"nodeCode\":\"ZBDECJ002\",\"indicatorIdentifierId\":165579,\"indicatorValueId\":90925,\"value\":0}]}";
Gson gson = new Gson();
Map<String, List<IndicatorPageDaoBO>> allMap = gson.fromJson(zhi, new TypeToken<Map<String, List<IndicatorPageDaoBO>>>() {}.getType());
System.out.println(JSONUtil.toJsonStr(allMap));
Map<String, Long> zhiMap = allMap.entrySet().stream()
.flatMap(entry -> entry.getValue().stream())
.collect(Collectors.groupingBy(
IndicatorPageDaoBO::getInstitutionCode,
Collectors.mapping(
IndicatorPageDaoBO::getProductCode,
Collectors.counting()
)
));
System.out.println("按机构分组统计每个机构下产品总数量,机构为key,总量为value"+JSONUtil.toJsonStr(zhiMap));
/**
Map<String, List<IndicatorPageDaoBO>> mapList
现在有一个map集合数据indicatorPageDaoBOS,map中value值List<IndicatorPageDaoBO>,IndicatorPageDaoBO对象中有属性institutionCode,productCode,nodeCode。最终实现结果如下:
解析mapList,解析其中的value值List<IndicatorPageDaoBO>,根据institutionCode,productCode两个属性分组统计数量,最终返回
Map<String,Long> zhiMap集合的结果,zhiMap中key为institutionCode+","+productCode这两个属性值的拼接,value是分组后统计的数量
*/
Map<String, Long> map = allMap.entrySet().stream()
.flatMap(entry -> entry.getValue().stream())
.collect(Collectors.groupingBy(
indicator -> String.format("%s,%s", indicator.getInstitutionCode(), indicator.getProductCode()),
Collectors.counting()
));
System.out.println("按机构和产品分组,逗号分隔机构和品作为key,value为统计总量"+JSONUtil.toJsonStr(map));
}
}