0

I am having a Order entity which contains list of items with in it, from that I need to loop list of orders and each order contains list of items, i need to extract Items alone from that items i need to extract single item quantity here my code

public class order{
    private Integer orderId;
    private String orderNo;
    .
    .
    private List<item> items;
    .
    .
    .
}

public class item{
    private Integer itemId;
    private String itemCode;
    private String itemDescription;
    private BigDecimal cost;
    .
    .
    .
}

How to iterate this in java8?

2 Answers 2

2

If you have a:

List<Order> list = ...

you could .flatMap() the nested list(s) of Item objects with:

List<Item> allItems = list.stream()
                          .map(Order::getItems)
                          .flatMap(List::stream)
                          .distinct()
                          .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

1

Your explanation is confusing.

If you have a single Order with Items that you need to loop over, it's really easy:

Order singleOrder;

singleOrder.getItems().stream()
    .//whatever else you need;

If you have a List of Orders, it's still really easy:

List<Order> multipleOrders;

multipleOrders.stream()
    .map(Order::getItems)
    .flatMap(List::stream)
    .//whatever else you need

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.