我有一个订单列表,我应该按两个标准进行分组。
Order_Id| Customer | Date | Amount |
1 | "Sam" | 2019-03-21 | 100 |
2 | "Nick" | 2019-03-21 | 102 |
3 | "Dan" | 2019-03-21 | 300 |
4 | "Sam" | 2019-04-21 | 400 |
5 | "Jenny" | 2019-04-21 | 220 |
6 | "Jenny" | 2019-04-12 | 330 |
应该找到每个月总金额的最高买家,对于当前示例:
{
MARCH: { customer='Dan', amount=300 },
APRIL: { customer='Jenny', amount=550 }
}
我找到了一个解决方案:
public class Main {
public static void main(String[] args) {
List<Order> orders = List.of(
new Order(1L, "Sam", LocalDate.of(2019, 3, 21), 100L),
new Order(2L, "Nick", LocalDate.of(2019, 3, 21), 102L),
new Order(3L, "Dan", LocalDate.of(2019, 3, 21), 300L),
new Order(4L, "Sam", LocalDate.of(2019, 4, 21), 400L),
new Order(5L, "Jenny", LocalDate.of(2019, 4, 21), 220L),
new Order(6L, "Jenny", LocalDate.of(2019, 4, 12), 330L)
);
solution1(orders);
}
private static void solution1(List<Order> orders) {
final Map<Month, Map<String, Long>> buyersSummed = new HashMap<>();
for (Order order : orders) {
Map<String, Long> customerAmountMap = buyersSummed.computeIfAbsent(order.getOrderMonth(), mapping -> new HashMap<>());
customerAmountMap.putIfAbsent(order.getCustomer(), 0L);
Long customerAmount = customerAmountMap.get(order.getCustomer());
customerAmountMap.put(order.getCustomer(), customerAmount + order.getAmount());
}
final Map<Month, BuyerDetails> topBuyers = buyersSummed.entrySet().stream()
.collect(
toMap(Entry::getKey, customerAmountEntry -> customerAmountEntry.getValue().entrySet().stream()
.map(entry -> new BuyerDetails(entry.getKey(), entry.getValue()))
.max(Comparator.comparingLong(BuyerDetails::getAmount)).orElseThrow())
);
}
}
一只名叫tom的猫
繁星淼淼
长风秋雁
慕娘9325324
www说
largeQ
相关分类