[JAVA 8使用谓词提取最新记录的流过滤器

问题描述 投票:2回答:1

我有一个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);
    }
java lambda java-8 java-stream predicate
1个回答
0
投票

您必须检查流中的所有元素。您可以将所有过滤的元素收集到LinkedList中并获取最后一个。

forloebs.stream()
.filter(predicate)
.collect(Collectors.toCollection(LinkedList::new)).peekLast();
© www.soinside.com 2019 - 2024. All rights reserved.