I have List<Item> that contains List<SubItem> objects inside and I need to return first date of SubItem. I'd like to use streams for this. I know that SubItem is never null or empty.
Logic behind is to retrieve date from SubItem which for all Item objects are the same. Loop through Item list then loop through SubItem (each Item has list of SubItem) and from the first SubItem return date.
Using non stream:
public LocalDate getSubItemDate(List<Item> items) {
LocalDate date = null;
for (Item item : items) {
List<SubItem> subItems = item.getSubItems().getSubItem();
for (SubItem si: subItems) {
XMLGregorianCalendar rawDate = si.getSubItemDate().getVal();
return rawDate.toGregorianCalendar().toZonedDateTime().toLocalDate();
}
}
return date;
}
And using stream I've tried so far and I want to have something like this:
// but it's boolean...
public LocalDate getSubItemDate(List<Item> items) {
return items
.stream()
.anyMatch(item ->
item.getSubItems().getSubItem()
.stream()
.anyMatch(si->
si.getSubItemDate().getVal().toGregorianCalendar().toZonedDateTime().toLocalDate()));