Reputation: 460
I am trying to convert code written in enhanced for each loop to streams in java. Here is my traditional code
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());
Upvotes: 0
Views: 132
Reputation: 826
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.
Upvotes: 1
Reputation: 3081
Following is about as Java-8 as this is going to get in my opinion.
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. This seems pointless on the face of it!
Upvotes: 1