假设这里的bean1和bean2是Java类,且bean2有一个属性字段,我们可以使用Java 8的Stream API来过滤列表。以下是一个示例代码:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Bean1 {
private List bean2List;
// 省略其他代码
public List filterBean2ListByPropertyValue(String propertyValue) {
return bean2List.stream()
.filter(bean2 -> bean2.getProperty().equals(propertyValue))
.collect(Collectors.toList());
}
}
public class Bean2 {
private String property;
// 省略其他代码
public String getProperty() {
return property;
}
}
public class Main {
public static void main(String[] args) {
Bean1 bean1 = new Bean1();
// 假设bean1有一个包含多个bean2对象的列表
List filteredList = bean1.filterBean2ListByPropertyValue("someValue");
// 打印过滤后的列表
filteredList.forEach(bean2 -> System.out.println(bean2.getProperty()));
}
}
在上面的示例中,我们假设Bean1类有一个名为bean2List的列表,其中包含多个Bean2对象。我们编写了一个名为filterBean2ListByPropertyValue的方法,该方法接受一个属性字段值作为参数,并使用Stream API对bean2List进行过滤,只保留属性字段值与传入参数相等的Bean2对象。最后,我们使用collect方法将过滤后的对象收集到一个新的列表中,并在主方法中打印这个列表。