Lambda equivalent for List <?> Compare using property A and return property B

private Date findRecordDate(List<DividendEvent> events, Scheme scheme) {
        for (DividendEvent event : events) {
            if (scheme.getName().equalsIgnoreCase(event.getScheme().getName())) {
                return event.getRecordDate();
            }
        }
        return null;
}

Any suggestions on what would be the lambda equivalent for the code above?

+4
source share
1 answer

First you need to filter the list and then display it, and then you can get the first occurrence.

private Date findRecordDate(List<DividendEvent> events, Scheme scheme) {
        String name = scheme.getName();
        return events.stream().filter(e -> name.equalsIgnoreCase(e.getScheme().getName()))
                              .map(Di‌​videndEvent::getReco‌​rdDate) 
                              .findFirst().orElse(null);
}
+4
source

Source: https://habr.com/ru/post/1678281/


All Articles