Last active
May 22, 2019 14:29
-
-
Save Gabbendorf/ffd8ba31e07f4662eda546b8416f10b3 to your computer and use it in GitHub Desktop.
Java Optional: example with .get() that might throw a NoSuchElementException
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String memberNameById(List<Member> members, int memberId) { | |
return members.stream() | |
.filter(member -> member.getId() == memberId) | |
.findFirst() // returns an Optional | |
.get() // extracts the value out of the Optional | |
.getName(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment