我正在为products
,attribute
和我提供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()
,我会收到以下错误。
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
}
}
答案 0 :(得分:4)
答案 1 :(得分:0)
所有流方法都返回另一个流。要转换回列表,您需要使用收集器:
<your stream>.collect(Collectors.toList());