0

what this method should look like in lambda expression ?

public Book returnBook(int idBook){
   for (Book b : records){
        if(b.getIdBook() == idBook){
            return b;
        }
    }

    return null;
}

1 Answer 1

2

The stream version would primarily be made of filter + findFirst

return records.stream()
              .filter(book -> book.getIdBook() == idBook)
              .findFirst()
              .orElse(null);
Sign up to request clarification or add additional context in comments.

1 Comment

or Optional<Book> returnBook(int idBook) works nicely as well :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.