如何使用Map中的键对对象数据进行累积乘法?

9w11ddsr  于 2022-10-15  发布在  Java
关注(0)|答案(0)|浏览(116)

这是Records类:

public class Records { 
    private BigDecimal price;
    private BigDecimal tos;
    private BigDecimal share;
}

Map<String, List<Records>>如果一个键有多个值,我想对这些值执行累积乘法,并为该键返回一个条目。
例如,在我的Map中,如果我存储数据,数据如下:
Map<String, List<Records>>有数据

{
"Id1": [ { "price": 3, "share": 4, "tos": 5}, { "price": 2, "share": 3, "tos": 6} ],
"Id2": [ { "price": 1, "share": 7, "tos": 4} ]
}

我想对Id1的数据执行累积乘法,并获得如下输出:
Map<String, Records>

{
"Id1": { "price": 6, "share": 12, "tos": 30},
"Id2": { "price": 1, "share": 7, "tos": 4}
}

这是我试过的密码

private static Map<String, Records> getCumulativeSafFafPaf(Map<String, List<Records>> recordsMap) {
    Map<String, Records> recordData = null;
    Records output = null;
    for (Map.Entry<String, List<Records>> data : recordsMap.entrySet()) {
        List<Records> dbData = data.getValue();
        for (String pid : recordsMap.keySet()){
            output = new Records();
            if(recordsMap.get(pid).size() > 1){
                BigDecimal price = BigDecimal.ONE, share = BigDecimal.ONE, tos= BigDecimal.ONE;
                for (Records abc : dbData){
                    price = DecimalUtil.resetValueWithDefaultScale(price .multiply(abc.getPrice()));
                    share = DecimalUtil.resetValueWithDefaultScale(share .multiply(abc.getShare()));
                    tos= DecimalUtil.resetValueWithDefaultScale(tos.multiply(abc.getTos()));
                }
                output.setPrice(price);
                output.setShare(share);
                output.setTos(tos);
            }
            recordData.put(pid, output);
        }
    }
    return recordData;
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题