我想准备一个简单的报告,显示按月显示的订购产品数量(降序):
Month Number of products
2018-10 8
2018-11 3
我的资料:
class Orders {
private List<Order> orders = new ArrayList<>();
public void prepareData() {
Product socks = new ProductBuilder()
.setPrice(new BigDecimal("23"))
.setCategory(Category.C)
.setName("SOCKS")
.build();
Product jacket = new ProductBuilder()
.setPrice(new BigDecimal("199"))
.setCategory(Category.A)
.setName("JACKET")
.build();
Product watch = new ProductBuilder()
.setPrice(new BigDecimal("100"))
.setCategory(Category.B)
.setName("WATCH CASIO")
.build();
Customer john = new CustomerBuilder()
.setAge(18)
.setName("JOHN")
.setSurname("JOHNSON")
.setEmail("[email protected]")
.build();
Customer mike = new CustomerBuilder()
.setAge(20)
.setName("MIKE")
.setSurname("MAX")
.setEmail("[email protected]")
.build();
Order orderJohn = new OrderBuilder()
.setQuantity(2)
.setCustomer(john)
.setProduct(watch)
.setOrderDate(LocalDate.now())
.build();
Order orderJohn2 = new OrderBuilder()
.setQuantity(4)
.setCustomer(john)
.setProduct(socks)
.setOrderDate(LocalDate.now())
.build();
Order orderMike = new OrderBuilder()
.setQuantity(2)
.setCustomer(mike)
.setProduct(jacket)
.setOrderDate(LocalDate.now())
.build();
orders.add(orderJohn);
orders.add(orderJohn2);
orders.add(orderMike);
}
}
现在,我使用
groupingBy
按月份获取订单数System.out.println(orders.stream()
.collect(Collectors
.groupingBy(e -> e.getOrderDate().withDayOfMonth(1), Collectors.counting())));
但是如何按月增加产品数量?
Quantity
类中的Order
字段。public class Order {
private Customer customer;
private Product product;
private int quantity;
private LocalDate orderDate;
//get/set
}
public class Customer {
private String name;
private String surname;
private int age;
private String email;
//get/set
}
public class Product {
private String name;
private BigDecimal price;
private Category category;
//get/set
}
和建设者
public class CustomerBuilder {
private Customer customer = new Customer();
public CustomerBuilder setName(String name){
customer.setName(name);
return this;
}
public CustomerBuilder setSurname(String surname){
customer.setSurname(surname);
return this;
}
public CustomerBuilder setAge(int age){
customer.setAge(age);
return this;
}
public CustomerBuilder setEmail(String email){
customer.setEmail(email);
return this;
}
public Customer build() {
return customer;
}
}
public class OrderBuilder {
private Order order = new Order();
public OrderBuilder setCustomer(Customer customer){
order.setCustomer(customer);
return this;
}
public OrderBuilder setProduct(Product product){
order.setProduct(product);
return this;
}
public OrderBuilder setQuantity(int quantity){
order.setQuantity(quantity);
return this;
}
public OrderBuilder setOrderDate(LocalDate orderDate){
order.setOrderDate(orderDate);
return this;
}
public Order build(){
return order;
}
}
public class ProductBuilder {
private Product product = new Product();
public ProductBuilder setCategory(Category category){
product.setCategory(category);
return this;
}
public ProductBuilder setName(String name){
product.setName(name);
return this;
}
public ProductBuilder setPrice(BigDecimal bigDecimal){
product.setPrice(bigDecimal);
return this;
}
public Product build() {
return product;
}
}
最佳答案
您可以将Collectors.counting()
替换为:
Collectors.summingInt(Order::getQuantity)
(它的工作方式是:
Collectors.reducing(0, Order::getQuantity, Integer::sum)
)编辑:
查看您对其他答案的评论,这是您需要的:
Map<LocalDate, IntSummaryStatistics> ord = orders.stream().collect(Collectors.groupingBy(e -> e.getOrderDate().withDayOfMonth(1), Collectors.summarizingInt(Order::getQuantity)));
ord.entrySet().stream().sorted(Map.Entry.comparingByValue((p1,p2) -> Long.compare(p2.getSum(), p1.getSum()))).forEach(System.out::println);