我正在进行一个邮局存储练习,其中部分方法使用另一个方法searchBoxes
,其参数是Predicate.我必须使用searchBoxes
来实现这些方法,但我不能像以前那样使用lambda表达式.
这是我必须用途:的searchBoxes
个方法
public List<Box> searchBoxes(Predicate<Box> predicate) {
if(predicate == null) {
throw new NullPointerException();
}
List<Box> selected = new LinkedList<>();
for(Box box : parcels) {
if(predicate.test(box)) {
selected.add(box);
}
}
return selected;
}
这是另一种方法:
public List<Box> getAllWeightLessThan(double weight) {
if(weight < 1) {
throw new IllegalArgumentException();
}
List<Box> result = searchBoxes(e -> e.getWeight() < weight);
return result;
}
我需要做的是避免使用lambda表达式,在这里我调用了searchBoxes
方法:searchBoxes(e -> e.getWeight() < weight);
,但问题是我必须使用searchBoxes
方法.
如何避免这种情况并以相同的方式调用该方法,但没有lambda呢?