https://git.ticatec.cn/henry.feng/test-order-processing
用poi技术读取Excel文件,再用逻辑判断数据合法性。
思路:利用Collectors.groupingBy查询重复数据,然后处理这些重复数据。分组的属性为:订单号、订单号+商品号,包括订单内容里面的收件人、收件人电话、收件人地址、邮政编码、发货人地址等,通过不同的组合筛选出重复数据,然后处理。
1、重复订单号
//订单号统计
Map<Object, Long> orderCountMap = orderListAll.stream().collect(
Collectors.groupingBy(o->o.getId(), Collectors.counting()));
System.out.println("各个订单的计数情况:" + orderCountMap);
//重复订单号
List<Object> orderList = orderCountMap.keySet().stream().
filter(key -> orderCountMap.get(key) > 1).collect(Collectors.toList());
//重复订单号
System.out.println("重复的的订单号: " + orderList);
打印结果示例:
各个订单的计数情况:{1005=2, 1004=1, 1003=1, 1002=1, 1001=1}
重复的的订单号: [1005]
2、同一个订单号重复的商品编码
先补全所有订单号,数据集为listAll,然后根据订单号+商品编码分组统计
//订单号和商品号重复的数据
Map<Object, Long> orderAndCodeCountMap = listAll.stream().collect(
Collectors.groupingBy(o->o.getId() + ":" + o.getProductCode(), Collectors.counting()));
System.out.println("重复订单号和商品号计数情况:" + orderAndCodeCountMap);
统计结果示例:
重复订单号和商品号计数情况:{1001:P1001=1, 1003:C3001=1, 1001:P1002=2, 1004:E4001=1, 1002:D2001=1, 1005:F6001=1, 1002:D2002=1, 1005:F5001=1, 1002:D2003=1}
重复的订单号和商品号: [1001:P1002]
3、根据其它属性的不同组合分组统计,找出订单号,如果单纯的订单号区分不开唯一数据,可以在源数据上补上一个序号属性,通过序号区分唯一数据。
你的要求是更改第一条订单号的状态,数据去重,保留的就是第一条订单号数据。
//所有订单,有重复
List<OrderDetail> orderListAll =
orderDetailList.stream().filter(o -> (!o.getId().equals("") && null != o.getId())).collect(Collectors.toList());
//不重复的订单(每个订单的第一条数据)
List<OrderDetail> orderListDistinct = orderListAll.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(OrderDetail::getId))),
ArrayList::new));
4、根据你判断出来的需要更改的订单数据,更新对应的状态。
自己试着编写比别人给你写出来你在去读懂他的代码来得快 ,这样你还可以学习知识
问题贴出来,bug在哪里,哪里
这么简单的面试都不会