title: JAVA 8 - Stream features giving error while retriving data from 3 POJO class
body: <p>I am having 3 POJO class for <code>products</code>, <code>attribute</code> &amp; <code>value</code>. All three POJOs are have relationship with other POJO class. Now, I want to retrive value for the
      each and every products.</p>
  
  <p>It was working perfectly with the older approach but now i want to modify my code with new JAVA 8 features.</p>
  
  <p>Please find my code below.</p>
  
  <pre><code>listProducts.stream().forEachOrdered(listproducts -&gt; 
  listproducts.getProductAttr()
                  .stream().forEachOrdered(attr -&gt; 
  (((Collection&lt;ProductAttrValue&gt;) attr.getProductAttrValue()
                  .stream().filter(attrlabel -&gt; 
  attrlabel.getLabel().equalsIgnoreCase("source"))))
                  .stream().forEachOrdered(attrval -&gt;
                                  {
                                      System.out.println(attrval.getValue());
                                  }
                              )       
                          )
  
              );
  </code></pre>
  
  <p>Please find the error message that I am getting.</p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: 
  java.util.stream.ReferencePipeline$2 cannot be cast to java.util.Collection
  at Hibernate.Testing.App.lambda$1(App.java:81)
  at java.util.Iterator.forEachRemaining(Unknown Source)
  at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown 
  Source)
  at java.util.stream.ReferencePipeline$Head.forEachOrdered(Unknown Source)
  at Hibernate.Testing.App.lambda$0(App.java:81)
  at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(Unknown Source)
  at java.util.stream.ReferencePipeline$Head.forEachOrdered(Unknown Source)
  at Hibernate.Testing.App.main(App.java:80)
  </code></pre>
  
  <p>If am not doing type casting <code>.stream().forEachOrdered(attr -&gt; ((Collection&lt;ProductAttrValue&gt;) attr.getProductAttrValue()</code> am getting the below errors.</p>
  
  <p><a href="https://i.stack.imgur.com/DGpRT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DGpRT.png" alt="enter image description here"></a>
  Please find my Product POJO class</p>
  
  <pre><code>public class Product {
  
      private String name;
      private long std_delivery_Time;
      private int min_order_Quantity;
  
      private String status;
      private long catalog_Id;
      private Date expiration_Date;
      private int rank;
      private String product_Type;
      private int is_Visible;
      private String product_Group;
         private String code;
  
  
      @Id
      @Column(name = "ID")
      @GeneratedValue(strategy=GenerationType.AUTO)
      private long id;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  
      public void setId(long id) {
          this.id = id;
      }
  
      public long getId() {
          return id;
      }
  
      public void setId(int id) {
            this.id = id;
           }
  
  
      public String getCode() {
        return code;
       }
  
  
      public void setCode(String code) {
        this.code = code;
       }
  
      @OneToMany( mappedBy = "product", cascade = CascadeType.ALL)
      private List&lt;ProductAttr&gt; productAttr;
      public List&lt;ProductAttr&gt; getProductAttr() {
          return productAttr;
      }
  
      public void setProductAttr(List&lt;ProductAttr&gt; productAttr) {
          this.productAttr = productAttr;
      }
   }
  </code></pre>
  
  <p>ProductAttr class</p>
  
  <pre><code>public class ProductAttr {
  
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "ID", updatable=false, nullable=false)
      private long id;
  
      @Column(name = "NAME")
      private String name;
  
      @Column(name = "PRODUCT_ID")
      private long product_id;
  
  
      public ProductAttr() {
          super();
          // TODO Auto-generated constructor stub
      }
  
      public void setId(long id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public long getProduct_id() {
          return product_id;
      }
  
      public void setProduct_id(long product_id) {
          this.product_id = product_id;
      }
  
      public long getId() {
          return id;
      }
  
      @ManyToOne
      @JoinColumn(name = "PRODUCT_ID",  referencedColumnName = "ID", 
  insertable = false, updatable = false)
      private Product product;
  
      public Product getProduct() {
          return product;
      }
  
      public void setProduct(Product product) {
          this.product = product;
      }
  
      @OneToMany( mappedBy = "productAttr", cascade = CascadeType.ALL)
      private Set&lt;ProductAttrValue&gt; productAttrValue;
  
          public Set&lt;ProductAttrValue&gt; getProductAttrValue() {
              return productAttrValue;
          }
  
          public void setProductAttrValue(Set&lt;ProductAttrValue&gt; 
  productAttrValue) {
              this.productAttrValue = productAttrValue;
          }
  
  }
  </code></pre>
  
  <p>ProductValue class</p>
  
  <pre><code>public class ProductAttrValue {
  
      @Id
      @Column(name = "ATTR_ID", updatable=false, nullable=false)
      private long attr_id;
  
      @Column(name = "LABEL")
      private String label;
  
      @Column(name = "VALUE")
      private String value; 
  
          public long getAttr_id() {
              return attr_id;
          }
  
          public void setAttr_id(long attr_id) {
              this.attr_id = attr_id;
          }
  
          public String getLabel() {
              return label;
          }
  
          public void setLabel(String label) {
              this.label = label;
          }
  
          public void setValue(String value) {
              this.value = value;
          }
  
          public String getValue() {
              return value;
          }
  
  
        @ManyToOne
        @JoinColumn(name = "ATTR_ID",  referencedColumnName = "ID", insertable 
        = false, updatable = false)
  
        private ProductAttr productAttr;
  
        public ProductAttr getProductAttr() {
              return productAttr;
        }
  
        public void setProductAttr(ProductAttr productAttr) {
              this.productAttr = productAttr;
          }
  
        public ProductAttrValue() {
          super();
          // TODO Auto-generated constructor stub
      }
  
  
  }
  </code></pre>