Skip to content

Instantly share code, notes, and snippets.

@Gabbendorf
Last active May 22, 2019 14:29
Show Gist options
  • Save Gabbendorf/ffd8ba31e07f4662eda546b8416f10b3 to your computer and use it in GitHub Desktop.
Save Gabbendorf/ffd8ba31e07f4662eda546b8416f10b3 to your computer and use it in GitHub Desktop.
Java Optional: example with .get() that might throw a NoSuchElementException
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