Skip to content

Instantly share code, notes, and snippets.

@Gabbendorf
Created May 22, 2019 14:41
Show Gist options
  • Save Gabbendorf/2f683781dce6f0696d5fcbb9b6b5774a to your computer and use it in GitHub Desktop.
Save Gabbendorf/2f683781dce6f0696d5fcbb9b6b5774a to your computer and use it in GitHub Desktop.
Java Optional: example with .ifPresent()
public void printMemberNameIfPresent(List<Member> members, int memberId) {
members.stream()
.filter(member -> member.getId() == memberId)
.findFirst()
.ifPresent(member -> printMemberFullName(member.getFullName())); // if present, apply the lambda on the value
}
private static void printMemberFullName(String memberFullName) {
System.out.println(String.format("The member's full name is %s", memberFullName));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment