JAVA 8 - Stream功能在从3个POJO类

时间:2018-01-10 09:12:00

标签: java-8 java-stream

我正在为productsattribute和我提供3个POJO课程。 value。所有三个POJO都与其他POJO类有关系。现在,我想重温价值     每一件产品。

它与旧方法完美配合,但现在我想用新的JAVA 8功能修改我的代码。

请在下面找到我的代码。

listProducts.stream().forEachOrdered(listproducts -> 
listproducts.getProductAttr()
                .stream().forEachOrdered(attr -> 
(((Collection<ProductAttrValue>) attr.getProductAttrValue()
                .stream().filter(attrlabel -> 
attrlabel.getLabel().equalsIgnoreCase("source"))))
                .stream().forEachOrdered(attrval ->
                                {
                                    System.out.println(attrval.getValue());
                                }
                            )       
                        )

            );

请找到我收到的错误消息。

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)

如果我没有进行类型转换.stream().forEachOrdered(attr -> ((Collection<ProductAttrValue>) attr.getProductAttrValue(),我会收到以下错误。

enter image description here 请找我的产品POJO课程

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<ProductAttr> productAttr;
    public List<ProductAttr> getProductAttr() {
        return productAttr;
    }

    public void setProductAttr(List<ProductAttr> productAttr) {
        this.productAttr = productAttr;
    }
 }

ProductAttr class

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<ProductAttrValue> productAttrValue;

        public Set<ProductAttrValue> getProductAttrValue() {
            return productAttrValue;
        }

        public void setProductAttrValue(Set<ProductAttrValue> 
productAttrValue) {
            this.productAttrValue = productAttrValue;
        }

}

ProductValue类

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
    }


}

2 个答案:

答案 0 :(得分:4)

你的缩进暗示了一种错误的心态。这些stream()操作不在同一级别。你有一个嵌套的操作: listProducts.stream()。forEachOrdered(listproducts - &gt;     listproducts.getProductAttr()。stream()。forEachOrdered(attr - &gt;         attr.getProductAttrValue()。流()                                   .filter(av - &gt; av.getLabel()。equalsIgnoreCase(“source”))                      /*.stream()*/.forEachOrdered(av - &gt; System.out.println(av.getValue()))     ) ); (我将误导性名称attrlabel和attrval更改为av,因为这些不是标签或值;您随后在这些变量上调用.getLabel()和.getValue(),因此我使用av代替“ProductAttrValue”) 过滤操作是唯一不嵌套的操作,因为它是链接到流的中间操作,并且终端操作只需链接到结果流。在那个地方没有必要的stream()调用(标有/*.stream()*/的那个)。 也就是说,你应该避免这种嵌套操作。 您可以使用 listProducts.stream()             .flatMap(listproducts - &gt; listproducts.getProductAttr()。stream())             .flatMap(attr - &gt; attr.getProductAttrValue()。stream())             .filter(av - &gt; av.getLabel()。equalsIgnoreCase(“source”))             .forEachOrdered(av - &gt; System.out.println(av.getValue())); 代替。

答案 1 :(得分:0)

所有流方法都返回另一个流。要转换回列表,您需要使用收集器:

<your stream>.collect(Collectors.toList());