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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.