Skip to content
Advertisement

Java8 how to get list of objects based on the value [closed]

Code is here order is having shipment and shipment having items and items having product

Order Class —————-

JavaScript

Shipment Class*

JavaScript

Item Class

JavaScript

Product

JavaScript

Here want to pass product name like mobile in to a method that return list of order which matches product name mobile.. Could you please help how we can right using streams in java8

Advertisement

Answer

It could be better to use flatMap for the inner lists:

JavaScript

Online demo

The chain of method map with method references may be replaced with a simple anyMatch:

JavaScript

Update

If the filtered orders must contain only the shipments with filtered items, this implies that the entire chain of objects and their containers needs to be recreated:

new Order with new list of Shipment -> new Shipment with new list of Items -> new Item with a copy of Product from the matching product (with “Mobile” name).

Assuming that all the relevant constructors have been provided, the orders with the filtered products may look as follows:

JavaScript

Output

JavaScript

Updated online demo

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement