I’ve a list of Order which I am fetching from some third part api.
List<Order> orders = fromApi(orderId);
Here is the Order class:
class Order{
Long orderId;
Product product
}
Similarly I fetch products also for those orders. Here the key is orderId.
List<Order> orderIds = orders.stream().map(Order::orderId).collect(Collectors.toList());
Map<Long,Product> userProductMap = fromAnotherApi(orderIds);
Here is Product class:
class Product{
String productDesc;
}
I want to extract a map with orderId as a key and Order as value(given below), but meanwhile also want to update orders with the product(from userProductMap) in same stream iteration.
Map<Long,Order> userOrderMap = orders.stream().collect(Collectors.toMap(order -> order.id, Function.identity()));
I want to provide some custom implementation instead of Function.identity() so that while putting Order as value in Map here, I take out product from the userProductMap and update in Order and use updated Order in Map as value.
13