Skip to content

Instantly share code, notes, and snippets.

@ybonnel
Created November 27, 2014 13:17
Show Gist options
  • Save ybonnel/fa4a1991a2f9af477941 to your computer and use it in GitHub Desktop.
Save ybonnel/fa4a1991a2f9af477941 to your computer and use it in GitHub Desktop.
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