简体   繁体   English

将每个循环的增强型转换为流 java 8

[英]Convert enhanced for each loop to streams java 8

I am trying to convert code written in enhanced for each loop to streams in java. Here is my traditional code我正在尝试将为每个循环编写的增强代码转换为 java 中的流。这是我的传统代码

    List<OrderDetail> orderDetails = new ArrayList<>();
    if (arInvoiceOrderResponseBody != null) {
        if (arInvoiceOrderResponseBody.getOrders() != null && 
         arInvoiceOrderResponseBody.getOrders().size() > 0) {
         for (OrderDetail orderDetail : arInvoiceOrderResponseBody.getOrders()) {
                if (orderDetail != null) {
                    if (orderDetail.getStatusHistory() != null && 
                        orderDetail.getStatusHistory().size() > 0) {
                        for (StatusHistory statusHistory : orderDetail.getStatusHistory()) {
                            if (statusHistory != null) {
                                if (statusHistory.getStatusCode() != null) {
                                    if (statusHistory.getStatusCode().equals("POD")) {
                                        orderDetail.setStatusDateTime(statusHistory.getStatus_date_time());
                                    }
                                }

                            }
                        }
                    }
                }
                orderDetails.add(orderDetail);
            }
            arInvoiceOrderResponseBody.setOrders(orderDetails);
        }

Can anyone help me on replicating same functionality through streams.Any help would be greatly helpful任何人都可以帮助我通过流复制相同的功能。任何帮助都会非常有帮助

This is what i am trying这就是我正在尝试的

    arInvoiceOrderResponseBody.getOrders().stream() .flatMap(order ->
    order.getStatusHistory().stream()) 
    .filter(statusHistory ->statusHistory.getStatusCode().equals("POD"))
    //Here if condition is true then i need to do this.I need to set one
    //of the property of main order object to one of the property of status 
    //history object 
    //order.setStatusDateTime(statusHistory.getStatus_date_time());

Following is about as Java-8 as this is going to get in my opinion.以下是我认为的 Java-8。

arInvoiceOrderResponseBody.getOrders().forEach(
      orderDetail -> orderDetail.getStatusHistory().stream()
        .filter(statusHistory -> "POD".equals(statusHistory.getStatusCode()))
        .findFirst()
        .ifPresent(statusHistory -> orderDetail.setStatusDateTime(statusHistory.getStatus_date_time()))
    );

I note the original code changes the existing OrderDetail instances but then puts them (all) into a new collection and replaces the original collection.我注意到原始代码更改了现有的OrderDetail实例,但随后将它们(全部)放入一个新集合并替换了原始集合。 This seems pointless on the face of it!从表面上看,这似乎毫无意义!

Splitting it out into multiple methods and breaking it down into two streams may work.将其拆分为多种方法并将其分解为两个流可能会起作用。 Something like:就像是:

  public static ResponseBody processResponseBody(ResponseBody responseBody)
  {
    if(validate(responseBody))
    {
      List<OrderDetail> orderDetails = responseBody.getOrders().stream()
          .filter(od -> validate(od))
          .map(od -> processOrderDetail(od))
          .collect(Collectors.toList());
      responseBody.setOrders(orderDetails);
    }
    
    return responseBody;
  }
  
  private static OrderDetail processOrderDetail(OrderDetail orderDetail)
  {
    StatusHistory statusHistory = orderDetail.getStatusHistory().stream()
          .filter(sh -> validate(sh))
          .findFirst()
          .orElseThrow(() -> new IllegalArgumentException("No Status History Found"));
    orderDetail.setStatusDateTime(statusHistory.getStatus_date_time());

    return orderDetail;
  }
  
  private static boolean validate(ResponseBody responseBody)
  {
    return responseBody != null && responseBody.getOrders() != null && responseBody.getOrders().size() > 0;
  }
  
  private static boolean validate(OrderDetail orderDetail)
  {
    return orderDetail != null && orderDetail.getStatusHistory() != null && orderDetail.getStatusHistory().size() > 0;
  }
  
  private static boolean validate(StatusHistory statusHistory)
  {
    return statusHistory != null && statusHistory.getStatusCode() != null && statusHistory.getStatusCode().equals("POD");
  }

I broke it into a stream to process the OrderDetail object and a stream to reduce the StatusHistory to a single object. I also broke the validation into their own methods for brevity and organization.我将其分解为 stream 以处理OrderDetail object 和 stream 以将StatusHistory减少为单个 object。为了简洁和组织,我还将验证分解为它们自己的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM