我有一个boolean类型的方法,该方法返回Predicate类型Incident的数据流,我只想过滤掉最新的事件。
public boolean hascompletededucation(LocalDate asOnDate) {
Predicate<Courses> predicate = e -> ((e.hasincident1(asOnDate));
return forloebs != null && forloebs.stream().anyMatch(predicate);
我尝试过
return forloebs != null && forloebs.stream().filter(predicate)
有点像,但是我想从hasincident1方法中获取最新事件。
方法hasincident1如下:
public boolean hasincident1(LocalDate asOnDate) {
Predicate<Incident> completeincident= e -> !e.isCancelled() && e.isCompleted()
&& (e.getDatoHaendelse().toLocalDate().isBefore(asOnDate)
|| e.getDateIncident().toLocalDate().isEqual(asOnDate));
return incidentList != null && incidentList.stream().anyMatch(completeincident);
}
您必须检查流中的所有元素。您可以将所有过滤的元素收集到LinkedList
中并获取最后一个。
forloebs.stream()
.filter(predicate)
.collect(Collectors.toCollection(LinkedList::new)).peekLast();