gpt4 book ai didi

java-8 - JAVA 8 - 从 3 个 POJO 类检索数据时出现错误的流功能

转载 作者:行者123 更新时间:2023-12-04 13:22:40 25 4
gpt4 key购买 nike

我有 3 个 POJO 类 products , attribute & 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 类

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
}


}

最佳答案

您的缩进表明错误的心态。这些stream()操作不在一个层次上。你有一个嵌套操作:

listProducts.stream().forEachOrdered(listproducts -> 
listproducts.getProductAttr().stream().forEachOrdered(attr ->
attr.getProductAttrValue().stream()
.filter(av -> av.getLabel().equalsIgnoreCase("source"))
/*.stream()*/.forEachOrdered(av -> System.out.println(av.getValue()))
)
);

(我将误导性名称 attrlabelattrval 更改为 av,因为它们不是标签或值;您正在调用 .getLabel().getValue() 随后 在这些变量上,所以我使用 av 作为“ ProductAttrValue")

filter 操作是唯一嵌套的操作,因为它是链接到流的中间操作,而终端操作只需链接到结果流。那个地方不需要 stream() 调用(用 /*.stream()*/ 标记的地方)。

也就是说,你应该避免这种嵌套操作。

你可以使用

listProducts.stream()
.flatMap(listproducts -> listproducts.getProductAttr().stream())
.flatMap(attr -> attr.getProductAttrValue().stream())
.filter(av -> av.getLabel().equalsIgnoreCase("source"))
.forEachOrdered(av -> System.out.println(av.getValue()));

相反。

关于java-8 - JAVA 8 - 从 3 个 POJO 类检索数据时出现错误的流功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48183832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com