

equals ( s )))) // of("foo").flatMap(s -> ofNullable("foo".equals(s))) => Optional System. equals ( s )))) // empty().flatMap(s -> ofNullable("foo".equals(s))) => Optional.empty System. map ( "foo" :: equals )) // ofNullable(null).map("foo"::equals) => Optional.empty System. println ( "ofNullable(null).map(\"foo\"::equals)" + " => " + ofNullable ( null ). map ( "foo" :: equals )) // of("foo").map("foo"::equals) => Optional System. map ( "foo" :: equals )) // empty().map("foo"::equals) => Optional.empty System. filter ( "foo" :: equals )) // ofNullable(null).filter("foo"::equals) => Optional.empty System. println ( "ofNullable(null).filter(\"foo\"::equals)" + " => " + ofNullable ( null ). filter ( "foo" :: equals )) // of("foo").filter("foo"::equals) => Optional System. filter ( "foo" :: equals )) // empty().filter("foo"::equals) => Optional.empty System.

If the predicate is true, return the Optional, other wise return empty OptionalĪpply the Function, and return the non-null result auto-wrapped as an Optional, or if null an empty OptionalĪpply the Optional-bearing Function, and return without wrapping as an Optional Get the underlying value, or if null, execute the Supplier function and throw the result Get the underlying value, or if null, execute the Supplier function and return the result Get the underlying value, or if null, return the argument. Get the underlying value, or if null, throw NoSuchElementException If the underlying value is not empty, execute the Consumer with it as an argument Javadoc: Ĭreate an optional with a possibly null reference Optional is a functional replacement for null. Java 8 in Action: Lambdas, streams, and functional-style programming by Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft.

