我的界面InterA:
public interface InterA
{
boolean check(Record line);
}
我的检查方法:
public class ClassA implements InterA{
@Override
public boolean check(Record line) {
if (condition) {
return true;
} else {
return false;
}
}
}
如何在仅包含过滤元素的下面的过滤方法中返回ClassB的新实例? 预先谢谢你。
public class ClassB {
List<Record> list;
public ClassB(List<Record> list) {
this.list = list;
}
public ClassB filter(InterA a) {
}
答案 0 :(得分:0)
使用Stream.filter()过滤与Record
匹配的criteria
。然后将过滤后的元素收集到列表(Stream.collect())中,并创建ClassB
的实例,将该列表传递到构造函数中。
public ClassB filter(InterA a) {
return new ClassB(
this.list
.stream()
.filter(a::check)
.collect(Collectors.toList())
);
}