Java Optional exercises
Implement findPhoneNumberByName in PhoneBook class that returns an optional. (No streams)
Implement findPhoneNumberByNameAndPunishIfNothingFound in PhoneBookCrawler that uses the implementation from exercise 1
Implement findPhoneNumberByNameAndPrintPhoneBookIfNothingFound in PhoneBookCrawler that uses the implementation from exercise 1
Did you receive a Hello message from the PhoneBook's toString method? If yes, reimplement the exercise 3 to avoid it get's printed. If no, reimplement exercise 3 to have it actually printed.
Can you reimplement exercise 2 using streams instead of using PhoneBook's findPhoneNumberByName
Can you reimplement exercise 3 using streams instead of using PhoneBook's findPhoneNumberByName
Implement findNameByPhoneNumber in PhoneBook class that returns an optional. Implement findPhoneNumberByNameOrNameByPhoneNumber in PhoneBookCrawler class. First search the phone book by name. If that returns nothing search the phone book by phone number. If that still returns nothing return the phone number of Jos de Vos.
- Create an optional
- A get on an empty optional leads to a NoSuchElementException
- orElseThrow
- orElse
- orElseGet
- Optionals are important assets in Streaming APIs
- Dodgy example with Java 8 Optionals that is tackled in Java 9+
- How to use optionals and how not to use optionals
- Best practices vs pitfalls
- Ideas?