除了在这些方法中调用的一个方法外,我有几个方法基本上都是相同的。
示例:
protected List < DeadlineEntity > getEntityOneDeadlines() {
return deadLineEntityList
.stream()
.filter(m - > getEntityOneDeadlineDbs().stream().anyMatch(p - > m.getDb_4() != null && m.getDb_4().equals(p)))
.collect(Collectors.toList());
}
protected List < DeadlineEntity > getEntityTwoDeadlines() {
return deadLineEntityList
.stream()
.filter(m - > getEntityTwoDeadlineDbs().stream().anyMatch(p - > m.getDb_5() != null && m.getDb_5().equals(p)))
.collect(Collectors.toList());
}
因此,唯一的区别是方法getDB()
。
由于我不想用这个方法10次,我想把它写进一个方法中,然后通过输入参数来控制它。
我的尝试看起来是这样的:
protected List < DeadLineEntity > getDeadlines(List < Integer > deadLineDbList, Function << ? super T, ? > dbProperty) {
return deadLineEntityList
.stream()
.filter(m - > deadLineDbList.stream().anyMatch(p - > ....))
}
在方法anymatch()
中,我没有得到进一步的结果。
这就是我想要使用它的方式:
List<DeadlineEntity> list1 = getDeadlines(getEntityOneDeadlineDbs(), EntityOne::getDb_4());
List<DeadlineEntity> list2 = getDeadlines(getEntityTwoDeadlineDbs(), EntityTwo::getDb_5());
你觉得怎么样?这是个好方法吗?getDeadlines()
方法中的进一步程序是什么
只需使用Supplier
插入所需的List
和Function
实例,即可将getter从DeadlineEntity
替换为符合返回列表的getEntityXXX
方法泛型类型的T
:
protected <T> List<DeadlineEntity> getDeadlines(Supplier<List<T>> dbProperty, Function<DeadlineEntity, T> getter) {
return deadLineEntityList
.stream()
.filter(m -> dbProperty.get()
.stream()
.anyMatch(p -> getter.apply(m) != null &&
getter.apply(m).equals(p)))
.collect(Collectors.toList());
}
List<DeadlineEntity> one = getDeadlines(this::getEntityOneDeadlineDbs, DeadlineEntity::getDb_4);
List<DeadlineEntity> two = getDeadlines(this::getEntityTwoDeadlineDbs, DeadlineEntity::getDb_5);
编辑:为了让代码更可读,我会先过滤掉所有等于null
的p
,然后简化lambda表达式,并将anyMatch
方法中equals
调用的参数切换为空安全:
protected <T> List<DeadlineEntity> getDeadlines(Supplier<List<T>> dbProperty, Function<DeadlineEntity, T> getter) {
return deadLineEntityList
.stream()
.filter(m -> dbProperty.get().stream()
.filter(Objects::nonNull)
.anyMatch(p -> p.equals(getter.apply(m))))
.collect(Collectors.toList());
}
filter
方法将Predicate
作为参数。因此,您可以将谓词与其他谓词分开。
protected List<DeadlineEntity> getEntityDeadlines(Predicate filterPredicate) {
return deadLineEntityList
.stream()
.filter(filterPredicate)
.collect(Collectors.toList());
}
protected List<DeadlineEntity> getEntityOneDeadlines() {
return getEntityDeadlines(getDb4Filter());
}
protected List<DeadlineEntity> getEntityTwoDeadlines() {
return getEntityDeadlines(getDb5Filter());
}
protected Predicate<DeadlineEntity> getDb4Filter() {
return m -> getEntityOneDeadlineDbs().stream().anyMatch(p -> m.getDb_4() != null && m.getDb_4().equals(p));
}
protected Predicate<DeadlineEntity> getDb5Filter() {
return m -> getEntityTwoDeadlineDbs().stream().anyMatch(p -> m.getDb_5() != null && m.getDb_5().equals(p));
}
也许将getDB的返回传递给您的方法会更容易一些?(如果总是相同的类型(。
如果您需要发送一个函数,它就像您应该使用Supplier<T>
而不是Function<? super T, ?>
一样,因为getBD没有参数。
要使用它,您必须使用Supplier类的get()
方法:
protected List<DeadLineEntity> getDeadlines(List<Integer> deadLineDbList, Supplier<T> dbProperty) {
return deadLineEntityList
.stream()
.filter(m -> deadLineDbList.stream()
.anyMatch(p -> dbProperty.get() != null && dbProperty.get().equals(p)))
.collect(Collectors.toList());
}