创建一个单独的谓词
Create a separate predicate
我的列表中有以下过滤器。我需要生活在指定时间范围内的人,其中两个列表中的 validTo 都是可选的。如您所见,它有点复杂,因为还有其他过滤器,我需要通过将谓词移动到变量来简化它。
people.stream()
.filter(person -> peopleTime.stream().anyMatch(time ->
(!person.getValidTo().isPresent() || time.getValidFrom().isBefore(person.getValidTo().get()) || time.getValidFrom().isEqual(person.getValidTo().get()))
&& (!time.getValidTo().isPresent() || time.getValidTo().get().isAfter(person.getValidFrom()) || time.getValidTo().get().isEqual(person.getValidFrom()))))
我试图创建一些 BiPredicate 并使用它,但 anyMatch 需要单个谓词。人 class 延长时间 class。
有什么帮助吗?
据我了解,你基本上有:
public abstract static class MyDate {
public abstract boolean isBefore(MyDate other);
public abstract boolean isAfter(MyDate other);
public abstract boolean isEqual(MyDate other);
}
public static abstract class Time {
public abstract Optional<MyDate> getValidTo();
public abstract Optional<MyDate> getValidFrom();
}
public static abstract class Person extends Time {
}
(好吧,我暂时离开实现)。
如果您创建以下 class :
public static class TimePersonPredicate implements Predicate<Time> {
private final Person person;
public TimePersonPredicate(Person person) {
this.person = person;
}
@Override
public boolean test(Time time) {
return (!person.getValidTo().isPresent() || time.getValidFrom().get().isBefore(person.getValidTo().get()) || time.getValidFrom().get().isEqual(person.getValidTo().get()))
&& (!time.getValidTo().isPresent() || time.getValidTo().get().isAfter(person.getValidFrom().get()) || time.getValidTo().get().isEqual(person.getValidFrom().get()));
}
}
您可以像这样缩短过滤行:
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
List<Time> peopleTime = new ArrayList<>();
people.stream()
.filter(person -> peopleTime.stream().anyMatch(new TimePersonPredicate(person) ))...
}
这是你想要的吗?
我的列表中有以下过滤器。我需要生活在指定时间范围内的人,其中两个列表中的 validTo 都是可选的。如您所见,它有点复杂,因为还有其他过滤器,我需要通过将谓词移动到变量来简化它。
people.stream()
.filter(person -> peopleTime.stream().anyMatch(time ->
(!person.getValidTo().isPresent() || time.getValidFrom().isBefore(person.getValidTo().get()) || time.getValidFrom().isEqual(person.getValidTo().get()))
&& (!time.getValidTo().isPresent() || time.getValidTo().get().isAfter(person.getValidFrom()) || time.getValidTo().get().isEqual(person.getValidFrom()))))
我试图创建一些 BiPredicate 并使用它,但 anyMatch 需要单个谓词。人 class 延长时间 class。
有什么帮助吗?
据我了解,你基本上有:
public abstract static class MyDate {
public abstract boolean isBefore(MyDate other);
public abstract boolean isAfter(MyDate other);
public abstract boolean isEqual(MyDate other);
}
public static abstract class Time {
public abstract Optional<MyDate> getValidTo();
public abstract Optional<MyDate> getValidFrom();
}
public static abstract class Person extends Time {
}
(好吧,我暂时离开实现)。
如果您创建以下 class :
public static class TimePersonPredicate implements Predicate<Time> {
private final Person person;
public TimePersonPredicate(Person person) {
this.person = person;
}
@Override
public boolean test(Time time) {
return (!person.getValidTo().isPresent() || time.getValidFrom().get().isBefore(person.getValidTo().get()) || time.getValidFrom().get().isEqual(person.getValidTo().get()))
&& (!time.getValidTo().isPresent() || time.getValidTo().get().isAfter(person.getValidFrom().get()) || time.getValidTo().get().isEqual(person.getValidFrom().get()));
}
}
您可以像这样缩短过滤行:
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
List<Time> peopleTime = new ArrayList<>();
people.stream()
.filter(person -> peopleTime.stream().anyMatch(new TimePersonPredicate(person) ))...
}
这是你想要的吗?