Defaulting Optional orElse with Optional.empty in Java 8

hotmeatballsoup :

Java 8 here. I need to search two lists of POJOs for a string and want to use the Stream/Optional APIs correctly.

If the name appears in the first list ("lunches") then I want to return an optional containing it. Else, if the name appears in the second list ("dinners") then I want to return an optional containing it. Otherwise I want to return Optional.empty() if the name doesn't existing in either list. My best attempt thus far:

public class Restaurant {

    private String id;
    private String name;
    private List<Food> lunches;
    private List<Food> dinners;

    public Optional<Food> findFoodByName(String name) {

        return Optional.of(lunches.stream()
                                  .filter(food -> food.getName()
                                                      .equalsIgnoreCase(name))
                                  .findFirst())
                       .orElse(dinners.stream()
                                      .filter(food -> food.getName()
                                                          .equalsIgnoreCase(name))
                       .findFirst());
//                     .orElse(null);        TODO: how to return empty optional if neither in 'lunches' nor 'dinners'?

    }

}

Can anyone help me cross the finish line here?

Deadpool :

Combine both the list using Stream.of and check for element or return Optional.empty()

Stream.of(lunches, dinners)
      .flatMap(List::stream)
      .filter(s -> s.getName()
                    .equalsIgnoreCase(name))
      .findFirst();

As per the suggestion from @Holger you can also use Stream.concat to concat two streams and then check for element

Stream.concat(lunches.stream(), dinners.stream())
      .filter(s -> s.getName()
                    .equalsIgnoreCase(name))
      .findFirst();

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=414597&siteId=1