【问题标题】:add an object in middle of the list based on the condition根据条件在列表中间添加一个对象
【发布时间】:2021-09-21 06:31:24
【问题描述】:

我有一个如下所示的排序列表

下面的代码我已经排序了。

             Comparator<MyBean> typeComparator = Comparator.comparing(MyBean::getType);

             Comparator<MyBean> costComparator = Comparator.comparing(MyBean::getCost);

             Comparator<MyBean> multipleFieldsComparator = typeComparator.thenComparing(costComparator); 
             
             Collections.sort(mbList, multipleFieldsComparator);

现在我用下面的代码确定每个Type 的总成本。

Map<String, Double> sum = mbList.stream().collect(
                        Collectors.groupingBy(MyBean::getType, Collectors.summingDouble(MyBean::getCost)));

这里我有每种类型的总成本。比如A = 109490.03和B = 4431.218

现在我需要在 mbList 列表的中间添加这个 Total Cost 对象。

我想要的输出应该如下所示

我的整个代码如下所示。

             Comparator<MyBean> typeComparator = Comparator.comparing(MyBean::getType);

             Comparator<MyBean> costComparator = Comparator.comparing(MyBean::getCost);

             Comparator<MyBean> multipleFieldsComparator = typeComparator.thenComparing(costComparator); 
             
             Collections.sort(mbList, multipleFieldsComparator);
             
             
             Map<String, Double> sum = mbList.stream().collect(
                        Collectors.groupingBy(MyBean::getType, Collectors.summingDouble(MyBean::getCost)));
            System.out.println(" $$$$$$$$$$$$$$$$$ "+sum);
            
            for (MyBean mb : mbList) {

                row = sheetType.createRow(sheetType.getPhysicalNumberOfRows());
                cell = row.createCell(cellnum++);
                cell.setCellValue((String) mb.getType() + "-" + mb.getCategory());
                cell = row.createCell(cellnum++);
                cell.setCellValue(mb.getCost());
                cellnum = 0;
            }

MyBean.java

public class MyBean{
    private String type;
    private Double total;
    private String xxx;
    private String yyy;

    //setters and getters
}

我的问题是如何在每个排序值的末尾添加Total cost,例如在A之后需要添加总成本,在B之后我们可以添加总成本......

【问题讨论】:

标签: java arraylist collections


【解决方案1】:

您快到了,但不是直接汇总总和,而是先按类型分组,然后计算每个子列表的总和:

a) 按类型分组

Map<String, List<MyBean>> byType = mbList.stream()
                                         .sorted(multipleFieldsComparator)
                                         .collect(
                                             Collectors.groupingBy(
                                                 MyBean::getType,
                                                 LinkedHashMap::new, // preserves order
                                                 Collectors.toList()
                                             ));

b) 计算每种类型的总和

for (Entry<String, List<MyBean>> entry : byType.entrySet()) {
    List<MyBean> typeList = entry.getValue();
    double sum = typeList.stream().mapToDouble(MyBean::getCost).sum();
    typeList.add(new MyBean(entry.getKey(), sum));
}

c) 将所有列表展平为一个大列表(它们已经正确排序)

List<MyBean> listWithSums = byType.values()
                                  .stream()
                                  .flatMap(List::stream)
                                  .collect(Collectors.toList());

【讨论】:

    【解决方案2】:

    这里是想法的快速草稿,肯定有比这更好的解决方案,但这会解决问题(稍后将更新此答案以改进代码)

    lastMatch = "", totalCost = 0;
    for ( mbList i = 0 -> n) {
        currentMatch = mbList[i].getType(); // return A- or B- ...
        if(currentMatch != lastMatch){
            if(lastMatch != ""){
                > print totalCost; // to avoid printing Total cost at first row
            }
            lastMatch = currentMatch;
            totalCost = 0;
        }
        > print col name = col value
        totalCost += mbList[i].getCost(); // keep adding cost for your currentRegExrMatch
    }
    

    【讨论】:

    • 我可以知道currentRegExprMatch是什么
    • 根据您的示例,它应该存储A- 以供首次运行,直到B- 出现。这个想法是为您的 A- 组计算成本并打印,直到它变为“B-”等等
    猜你喜欢
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    相关资源
    最近更新 更多