根据条件在列表中间添加一个对象
add an object in middle of the list based on the condition
我有一个如下所示的排序列表
我用下面的代码排序了。
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
之后我们可以添加总计成本....
这里是想法的快速草稿,肯定有比这更好的解决方案,但这可以解决问题(稍后将更新此答案以改进代码)
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
}
你就快完成了,但不是直接汇总总和,而是先按类型分组,然后计算每个子列表的总和:
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());
我有一个如下所示的排序列表
我用下面的代码排序了。
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
之后我们可以添加总计成本....
这里是想法的快速草稿,肯定有比这更好的解决方案,但这可以解决问题(稍后将更新此答案以改进代码)
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
}
你就快完成了,但不是直接汇总总和,而是先按类型分组,然后计算每个子列表的总和:
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());