Created
May 22, 2019 14:41
-
-
Save Gabbendorf/2f683781dce6f0696d5fcbb9b6b5774a to your computer and use it in GitHub Desktop.
Java Optional: example with .ifPresent()
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 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