fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.stream.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. public class Main
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. List<Order> orderList = new ArrayList<Order>();
  14. List<Shipment> shipmentList = new ArrayList<Shipment>();
  15. List<Item> itemList = new ArrayList<Item>();
  16.  
  17. Shipment shipment = new Shipment();
  18. shipment.setItem(itemList);
  19. shipmentList.add(shipment);
  20.  
  21. Order order = new Order();
  22. order.setShipment(shipmentList);
  23. orderList.add(order);
  24.  
  25. List<Shipment> shipmentList2 = new ArrayList<Shipment>();
  26. List<Item> itemList2 = new ArrayList<Item>();
  27.  
  28. Shipment shipment2 = new Shipment();
  29. shipment2.setItem(itemList2);
  30. shipmentList2.add(shipment2);
  31.  
  32. Order order2 = new Order();
  33. order2.setShipment(shipmentList2);
  34. orderList.add(order2);
  35.  
  36. List<Shipment> shipmentList3 = new ArrayList<Shipment>();
  37. List<Item> itemList3 = new ArrayList<Item>();
  38.  
  39. Shipment shipment3 = new Shipment();
  40. shipment3.setItem(itemList3);
  41. shipmentList3.add(shipment3);
  42.  
  43. Order order3 = new Order();
  44. order3.setShipment(shipmentList3);
  45. orderList.add(order3);
  46.  
  47. Item item = new Item();
  48. Item item1 = new Item();
  49. Item item2 = new Item();
  50. Item item3 = new Item();
  51. Item item4 = new Item();
  52. Item item5 = new Item();
  53. Item item6 = new Item();
  54. Item item7 = new Item();
  55.  
  56. Product product = new Product();
  57. product.setName("Mobile");
  58. Product product1 = new Product();
  59. product1.setName("Mobile");
  60. Product product2 = new Product();
  61. product2.setName("Tv");
  62. Product product3 = new Product();
  63. product3.setName("AC");
  64. Product product4 = new Product();
  65. product4.setName("Tab");
  66. Product product5 = new Product();
  67. product5.setName("Bike");
  68. Product product6 = new Product();
  69. product6.setName("Bike");
  70. Product product7 = new Product();
  71. product7.setName("Bike");
  72.  
  73. item.setProduct(product);
  74. item1.setProduct(product1);
  75. item2.setProduct(product2);
  76. item3.setProduct(product3);
  77. item4.setProduct(product4);
  78. item5.setProduct(product5);
  79. item6.setProduct(product7);
  80. item7.setProduct(product7);
  81.  
  82. itemList.add(item);
  83. itemList2.add(item1);
  84. itemList2.add(item2);
  85. itemList3.add(item3);
  86. itemList3.add(item4);
  87. itemList2.add(item5);
  88. itemList3.add(item6);
  89. itemList.add(item7);
  90.  
  91. orderList.forEach(System.out::println);
  92. System.out.println("-----------");
  93.  
  94. List<Order> filteredOrders = orderList.stream()
  95. .filter(o -> o.getShipment().stream()
  96. .flatMap(s -> s.getItem().stream()) // stream of Item
  97. .map(i -> i.getProduct().getName()) // stream of product
  98. //.map(Product::getName) // stream of product names
  99. .anyMatch("Mobile"::equals)
  100. )
  101. .collect(Collectors.toList());
  102.  
  103. filteredOrders.forEach(System.out::println);
  104. }
  105. }
  106.  
  107. class Order {
  108.  
  109. private List<Shipment> shipment;
  110.  
  111. public List<Shipment> getShipment() {
  112. return shipment;
  113. }
  114.  
  115. public void setShipment(List<Shipment> shipment) {
  116. this.shipment = shipment;
  117. }
  118. @Override
  119. public String toString() {
  120. return "order: shipments=" + shipment;
  121. }
  122. }
  123.  
  124. class Shipment {
  125.  
  126. private List<Item> item;
  127.  
  128. public List<Item> getItem() {
  129. return item;
  130. }
  131.  
  132. public void setItem(List<Item> item) {
  133. this.item = item;
  134. }
  135.  
  136. @Override
  137. public String toString() {
  138. return "items=" + item;
  139. }
  140. }
  141.  
  142. class Item {
  143.  
  144. private Product product;
  145.  
  146. public Product getProduct() {
  147. return product;
  148. }
  149.  
  150. public void setProduct(Product product) {
  151. this.product = product;
  152. }
  153.  
  154. @Override
  155. public String toString() {
  156. return ":product=" + product;
  157. }
  158. }
  159.  
  160. class Product {
  161.  
  162. private String name;
  163.  
  164. public String getName() {
  165. return name;
  166. }
  167.  
  168. public void setName(String name) {
  169. this.name = name;
  170. }
  171.  
  172. @Override
  173. public String toString() {
  174. return "product.name=" + name;
  175. }
  176. }
Success #stdin #stdout 0.11s 36800KB
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]]