Created
November 27, 2014 13:17
-
-
Save ybonnel/fa4a1991a2f9af477941 to your computer and use it in GitHub Desktop.
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
import java.util.Optional; | |
public class TestOption { | |
public static void main(String[] args) { | |
// Avec option | |
System.out.println(getOption() | |
.filter(c -> c.age < 35) | |
.map(c -> "Age = " + c.age) | |
.orElse("Contact not found")); | |
// Sans option | |
Contact c = get(); | |
if (c != null && c.age < 35) { | |
System.out.println("Age = " + c.age); | |
} else { | |
System.out.println("Contact not found"); | |
} | |
} | |
public static Contact get() { | |
return null; | |
} | |
public static Optional<Contact> getOption() { | |
return Optional.empty(); | |
} | |
public static class Contact { | |
public int age; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment