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
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(DividendEvent::getRecordDate)
.findFirst().orElse(null);
}
+4