【发布时间】:2020-09-09 10:58:13
【问题描述】:
groupby如何使用计数功能?
我有以下数据,我想要以下输出:
期望的输出:
Stock | TOTAL | INPROGRESS | CANCEL | COMPLETED
-----------------------------------------------
HCL | 4 | 2 | 1 | 1
TCS | 5 | 2 | 0 | 2
注意:我将这些数据存储在 hashmap 中。键:OrderId,值:OrderModel(PairName,状态)
我可以用下面的代码来实现。但我想要更正确的代码。如果您有其他方法,请建议我。
public static void main(String[] args) {
Map<Integer, Model> test = new HashMap<Integer, Model>();
Model m1 = new Model("HCL", "Inprogress");
Model m2 = new Model("HCL", "Cancel");
Model m3 = new Model("HCL", "Inprogress");
Model m4 = new Model("HCL", "Completed");
Model a1 = new Model("TCS", "Inprogress");
Model a2 = new Model("TCS", "Inprogress");
Model a3 = new Model("TCS", "Inprogress");
Model a4 = new Model("TCS", "Completed");
Model a5 = new Model("TCS", "Completed");
int count = 1;
test.put(count++, m1);
test.put(count++, m2);
test.put(count++, m3);
test.put(count++, m4);
test.put(count++, a1);
test.put(count++, a2);
test.put(count++, a3);
test.put(count++, a4);
test.put(count, a5);
Map<String, Model> countPair = new HashMap<String, Model>();
test.forEach((k, v) -> System.out.println("Key:" + k + " Pair:" + v.getName() + " Status:" + v.getStatus()));
System.out.println(" With Count !!!!");
List<Model> list = new ArrayList<Model>();
test.entrySet().stream().forEach(e -> list.add(e.getValue()));
// Total
Map<String, Long> counted = list.stream().collect(Collectors.groupingBy(Model::getName, Collectors.counting()));
counted.forEach((k, v) -> {
countPair.put(k, new Model.ModelBuilder().setTotal(v).build());
});
// Inprogress
counted = list.stream().filter(e -> e.getStatus().equals("Inprogress"))
.collect(Collectors.groupingBy(Model::getName, Collectors.counting()));
counted.forEach((k, v) -> countPair.get(k).setInProgressCount(v));
// Cancel
counted = list.stream().filter(e -> e.getStatus().equals("Cancel"))
.collect(Collectors.groupingBy(Model::getName, Collectors.counting()));
counted.forEach((k, v) -> countPair.get(k).setCancelCount(v));
// Completed
counted = list.stream().filter(e -> e.getStatus().equals("Completed"))
.collect(Collectors.groupingBy(Model::getName, Collectors.counting()));
counted.forEach((k, v) -> countPair.get(k).setCompletedCount(v));
countPair.forEach((k, v) -> System.out.println("Pair : " + k + " : " + v.getTotal() + " , "
+ v.getInProgressCount() + " , " + v.getCancelCount() + " , " + v.getCompletedCount()));
}
型号:
public class Model {
private String name;
private String status;
private long total;
private long inProgressCount;
private long completedCount;
private long cancelCount;
static class ModelBuilder {
private long total;
private long inProgressCount;
private long completedCount;
private long cancelCount;
public ModelBuilder setTotal(long total) {
this.total = total;
return this;
}
public ModelBuilder setInProgressCount(long inProgressCount) {
this.inProgressCount = inProgressCount;
return this;
}
public ModelBuilder setCompletedCount(long completedCount) {
this.completedCount = completedCount;
return this;
}
public ModelBuilder setCancelCount(long cancelCount) {
this.cancelCount = cancelCount;
return this;
}
public Model build() {
return new Model(this);
}
}
public Model(ModelBuilder modelBuilder) {
this.total = modelBuilder.total;
this.inProgressCount = modelBuilder.inProgressCount;
this.completedCount = modelBuilder.completedCount;
this.cancelCount = modelBuilder.cancelCount;
}
public Model(String name, String status) {
super();
this.name = name;
this.status = status;
}
//getter and setter
}
【问题讨论】:
-
"我可以使用以下代码实现。但我想要更正确的代码" 那么您的代码是否有效?如果它不起作用,你真的能说你已经“达到”了你的目标吗?我想你真正想要的是更简洁代码之类的?
标签: java performance java-stream