fork download
  1. import java.util.*;
  2. import java.util.stream.*;
  3.  
  4.  
  5. public class Main
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. List<Order> orderList = new ArrayList<Order>();
  10. List<Shipment> shipmentList = new ArrayList<Shipment>();
  11. List<Item> itemList = new ArrayList<Item>();
  12.  
  13. Shipment shipment = new Shipment();
  14. shipment.setItem(itemList);
  15. shipmentList.add(shipment);
  16.  
  17. Order order = new Order();
  18. order.setShipment(shipmentList);
  19. orderList.add(order);
  20.  
  21. List<Shipment> shipmentList2 = new ArrayList<Shipment>();
  22. List<Item> itemList2 = new ArrayList<Item>();
  23.  
  24. Shipment shipment2 = new Shipment();
  25. shipment2.setItem(itemList2);
  26. shipmentList2.add(shipment2);
  27.  
  28. Order order2 = new Order();
  29. order2.setShipment(shipmentList2);
  30. orderList.add(order2);
  31.  
  32. List<Shipment> shipmentList3 = new ArrayList<Shipment>();
  33. List<Item> itemList3 = new ArrayList<Item>();
  34.  
  35. Shipment shipment3 = new Shipment();
  36. shipment3.setItem(itemList3);
  37. shipmentList3.add(shipment3);
  38.  
  39. Order order3 = new Order();
  40. order3.setShipment(shipmentList3);
  41. orderList.add(order3);
  42.  
  43. Item item = new Item();
  44. Item item1 = new Item();
  45. Item item2 = new Item();
  46. Item item3 = new Item();
  47. Item item4 = new Item();
  48. Item item5 = new Item();
  49. Item item6 = new Item();
  50. Item item7 = new Item();
  51.  
  52. Product product = new Product();
  53. product.setName("Mobile");
  54. Product product1 = new Product();
  55. product1.setName("Mobile");
  56. Product product2 = new Product();
  57. product2.setName("Tv");
  58. Product product3 = new Product();
  59. product3.setName("AC");
  60. Product product4 = new Product();
  61. product4.setName("Tab");
  62. Product product5 = new Product();
  63. product5.setName("Bike");
  64. Product product6 = new Product();
  65. product6.setName("Bike");
  66. Product product7 = new Product();
  67. product7.setName("Bike");
  68.  
  69. item.setProduct(product);
  70. item1.setProduct(product1);
  71. item2.setProduct(product2);
  72. item3.setProduct(product3);
  73. item4.setProduct(product4);
  74. item5.setProduct(product5);
  75. item6.setProduct(product7);
  76. item7.setProduct(product7);
  77.  
  78. itemList.add(item);
  79. itemList2.add(item1);
  80. itemList2.add(item2);
  81. itemList3.add(item3);
  82. itemList3.add(item4);
  83. itemList2.add(item5);
  84. itemList3.add(item6);
  85. itemList.add(item7);
  86.  
  87. orderList.forEach(System.out::println);
  88. System.out.println("-----------");
  89.  
  90. List<Order> filteredOrders = orderList
  91. .stream()
  92. .filter(o -> o.getShipment().stream()
  93. .flatMap(s -> s.getItem().stream()) // stream of Item
  94. .map(i -> i.getProduct().getName()) // stream of product
  95. //.map(Product::getName) // stream of product names
  96. .anyMatch("Mobile"::equals)
  97. )
  98. .collect(Collectors.toList());
  99.  
  100. filteredOrders.forEach(System.out::println);
  101.  
  102. List<Order> filteredOrderProducts = orderList
  103. .stream()
  104. .filter(o ->
  105. o.getShipment().stream()
  106. .flatMap(s -> s.getItem().stream()) // stream of Item
  107. .anyMatch(i -> "Mobile".equals(i.getProduct().getName()))
  108. )
  109. .map(o -> new Order(
  110. o.getShipment().stream()
  111. .map(s -> new Shipment(
  112. s.getItem().stream()
  113. .filter(i -> "Mobile".equals(i.getProduct().getName()))
  114. .map(i -> new Item(i))
  115. .collect(Collectors.toList())
  116. ))
  117. .collect(Collectors.toList())
  118. ))
  119. .collect(Collectors.toList());
  120. System.out.println("----\norders with filtered products");
  121. filteredOrderProducts.forEach(System.out::println);
  122. }
  123. }
  124.  
  125. class Order {
  126. public Order(List<Shipment> shipments) {
  127. this.shipment = shipments;
  128. }
  129.  
  130. public Order() {
  131. this(new ArrayList<>());
  132. }
  133.  
  134. private List<Shipment> shipment;
  135.  
  136. public List<Shipment> getShipment() {
  137. return shipment;
  138. }
  139.  
  140. public void setShipment(List<Shipment> shipment) {
  141. this.shipment = shipment;
  142. }
  143. @Override
  144. public String toString() {
  145. return "order: shipments=" + shipment;
  146. }
  147. }
  148.  
  149. class Shipment {
  150.  
  151. private List<Item> item;
  152.  
  153. public Shipment(List<Item> items) {
  154. this.item = items;
  155. }
  156.  
  157. public Shipment() {
  158. this(new ArrayList<>());
  159. }
  160.  
  161. public List<Item> getItem() {
  162. return item;
  163. }
  164.  
  165. public void setItem(List<Item> item) {
  166. this.item = item;
  167. }
  168.  
  169. @Override
  170. public String toString() {
  171. return "items=" + item;
  172. }
  173. }
  174.  
  175. class Item {
  176.  
  177. private Product product;
  178.  
  179. public Item(Item item) {
  180. this(new Product(item.getProduct()));
  181. }
  182.  
  183. public Item(Product product) {
  184. this.product = product;
  185. }
  186.  
  187. public Item() {
  188. this((Product)null);
  189. }
  190.  
  191. public Product getProduct() {
  192. return product;
  193. }
  194.  
  195. public void setProduct(Product product) {
  196. this.product = product;
  197. }
  198.  
  199. @Override
  200. public String toString() {
  201. return ":product=" + product;
  202. }
  203. }
  204.  
  205. class Product {
  206.  
  207. private String name;
  208.  
  209. public Product(Product p) {
  210. this(p.getName());
  211. }
  212.  
  213. public Product(String name) {
  214. this.name = name;
  215. }
  216.  
  217. public Product() {
  218. this("Unknown");
  219. }
  220.  
  221. public String getName() {
  222. return name;
  223. }
  224.  
  225. public void setName(String name) {
  226. this.name = name;
  227. }
  228.  
  229. @Override
  230. public String toString() {
  231. return "product.name=" + name;
  232. }
  233. }
Success #stdin #stdout 0.11s 36684KB
stdin
Standard input is empty
stdout
order: shipments=[items=[:product=product.name=Mobile, :product=product.name=Bike]]
order: shipments=[items=[:product=product.name=Mobile, :product=product.name=Tv, :product=product.name=Bike]]
order: shipments=[items=[:product=product.name=AC, :product=product.name=Tab, :product=product.name=Bike]]
-----------
order: shipments=[items=[:product=product.name=Mobile, :product=product.name=Bike]]
order: shipments=[items=[:product=product.name=Mobile, :product=product.name=Tv, :product=product.name=Bike]]
----
orders with filtered products
order: shipments=[items=[:product=product.name=Mobile]]
order: shipments=[items=[:product=product.name=Mobile]]