猿问

使用流 Java 8 从 List<Object>,List<Object> 转换为 List

我有 2 个相同类型的列表说


class Orders {

    Strig User;

    String tradeDate;

    BigDecimal Qty;

}

比较后,我想转换为另一个对象的列表说


class DiffOrders {

        String User;

        String tradeDate;

        BigDecimal currentQty;

        BigDecimal prevQty;

    }

我有两个订单列表


List<Orders>currentOrders;

List<Orders>prevOrders;


   List<Orders> DiffOrders = current.stream()

                                       .filter(curr->previous.stream().anyMatch(prev->OrderPredicate.orderDiffSize(curr, prev)))

                                       .collect(Collectors.toList());

对于同一日期和同一用户的订单,我想捕获匹配订单中的数量。


我能够找到List<Orders>匹配的。但不确定如何捕获并转换为List<DiffOrders>新类的。能否请你帮忙?


编辑 1:OrderPredicate.orderDiffSize 是一个简单的函数,用于比较当前和先前订单的用户、交易日期、数量和符号(+ 代表卖出,- 代表买入)。为简洁起见,此处未提供。


编辑 2:上一个/当前列表的大小是合理的,可以忽略 o log n 计算问题。


编辑 3:删除方向以保持简单。例如,我分别在 01-Jul、02-Jul、03-Jul 的 prevOrders 和 CurrentOrders 中有订单。如果同一日期的订单数量不同,我想将其放入 DiffOrders 中,其中包含当前和之前的数量。希望这能说明问题。


慕尼黑8549860
浏览 119回答 3
3回答

森栏

听起来这个问题的症结在于您想要省略 中current没有匹配项的任何项目previous,但使用 中的实际对应值转换确实有匹配项previous的项目。在那种情况下flatMap就是你想要的。您可以使用flatMap将每个Order不转换为DiffOrders,而是转换为一个流,DiffOrders对于不匹配项,该流将是空的,并且由单个元素组成,用于匹配项。List<DiffOrders> matchedOrders = current.stream()&nbsp; &nbsp; .flatMap(curr -> {&nbsp; &nbsp; &nbsp; Optional<Order> p = previous.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(prev -> OrderPredicate.orderSameDirectionAndSize(curr, prev))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findFirst();&nbsp; &nbsp; &nbsp; if (p.isPresent() && !p.get().qty.equals(curr.qty)) {&nbsp; &nbsp; &nbsp; &nbsp; return Stream.of(new DiffOrders(curr.user, curr.tradeDate, curr.qty, p.get().qty));&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return Stream.empty();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })&nbsp;&nbsp; &nbsp; .collect(Collectors.toList());

12345678_0001

据我了解,这个问题的主要需求之一是 current 和 previous 的映射Order,这样您就可以MatchOrder从细节中构建出来。当然,这将需要在构建此类当前项到先前项的过程map中进行操作。filterList<MatchOrders> matchedOrders = currentOrders.stream()&nbsp; &nbsp; &nbsp; &nbsp; .map(curr -> new AbstractMap.SimpleEntry<>(curr, prevOrders.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(prev -> orderSameDirectionAndSize(curr, prev))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findAny())) // <Order, Optional<Order>> currentToOptionalPrevious&nbsp; &nbsp; &nbsp; &nbsp; .map(e -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Orders current = e.getKey();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Orders previous = e.getValue().orElse(current); // assumed a fallback&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new MatchOrders(current.getUser(), current.getQty(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; previous.getQty(), current.getDirection(), previous.getDirection());&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());

人到中年有点甜

对于简单的代码和简单的算法分两步就可以解决;private List<DiffOrders>&nbsp; mergeLists(List<Orders> currentOrders, List<Orders> prevOrders) {&nbsp; &nbsp; Map<String, List<Orders>> map = prevOrders.stream().collect(Collectors.groupingBy(order -> order.getUser() + order.getTradeDate()));&nbsp; &nbsp; return currentOrders.stream().filter(current ->{&nbsp; &nbsp; &nbsp; &nbsp;String key = current.getUser() + current.getTradeDate();&nbsp; &nbsp; &nbsp; &nbsp;return map.get(key) != null && !map.get(key).isEmpty();&nbsp; &nbsp;}).map(current -> mapper(current, map.get(current.getUser() + current.getTradeDate()))).collect(Collectors.toList());}private DiffOrders mapper(Orders current, List<Orders> list) {&nbsp; &nbsp; DiffOrders diffOrders = new DiffOrders();&nbsp; &nbsp; diffOrders.setCurrentQty(current.getQty());&nbsp; &nbsp; diffOrders.setTradeDate(current.getTradeDate());&nbsp; &nbsp; diffOrders.setUser(current.getUser());&nbsp; &nbsp; diffOrders.setPrevQty(list.get(0).getQty());&nbsp; &nbsp; return diffOrders;}
随时随地看视频慕课网APP

相关分类

Java
我要回答