In Java 9 version, the Optional.ifPresentOrElse() and Optional.or() methods were introduced to improve their functionality. Optional.ifPresentOrElse()The method checks if the value exists, if it exists then applies the operation with the value, otherwise returns a no operation, while the Optional.or() method checks if the value exists, if Returns the option containing the value if present, otherwise returns the Optional applied to the Supplier function. The Optional.ifPresentOrElse() method contains two parameters, Consumer and Runnable, while the Optional.or() method contains only one parameter , Supplier. The syntax of
<strong>public void ifPresentOrElse(Consumer<? super T><!--? super T--> action, Runnable emptyAction)</strong>
import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; public class IfPresentOrElseMethodTest { public static void main(String args[]) { optionalIsPresent(); optionalIsEmpty(); } private static void optionalIsPresent() { <strong>Optional<String></strong> optional = <strong>Optional.of</strong>("Test"); AtomicInteger successCounter = new AtomicInteger(); AtomicInteger emptyOptionalsCounter = new AtomicInteger(); optional.<strong>ifPresentOrElse</strong>(x -> successCounter.incrementAndGet(), () -> emptyOptionalsCounter.incrementAndGet()); System.out.println(successCounter.get()); System.out.println(emptyOptionalsCounter.get()); } private static void optionalIsEmpty() { Optional optional = <strong>Optional.empty()</strong>; AtomicInteger successCounter = new AtomicInteger(); AtomicInteger emptyOptionalsCounter = new AtomicInteger(); optional.<strong>ifPresentOrElse</strong>(x -> successCounter.incrementAndGet(), () -> emptyOptionalsCounter.incrementAndGet()); System.out.println(successCounter.get()); System.out.println(emptyOptionalsCounter.get()) } }
<strong>1 0 0 1</strong>
<strong>public Optional<T> or(Supplier<? extends Optional<? extends T<!--? extends Optional<? extends T-->>> supplier)</strong>
import java.util.Optional; public class OptionalOrMethodTest { public static void main(String args[]) { optionalIsPresent(); optionalIsEmpty(); } private static void optionalIsPresent() { <strong>Optional<String></strong> optional1 = <strong>Optional.of</strong>("Test1"); <strong>Optional<String></strong> optional2 = <strong>Optional.of</strong>("Test2"); <strong>Optional<String</strong><strong>></strong> optional3 = <strong>optional1.or</strong>(() -> optional2); System.out.println(optional3.get()); } private static void optionalIsEmpty() { <strong>Optional<String></strong> optional1 = <strong>Optional.empty()</strong>; <strong>Optional<String></strong> optional2 = <strong>Optional.of</strong>("Test2"); <strong>Optional<String></strong> optional3 = <strong>optional1.or</strong>(() -> optional2); System.out.println(optional3.get()); } }
<strong>Test1 Test2</strong>
The above is the detailed content of What is the difference between Optional.ifPresentOrElse() and Optional.or() methods in Java 9?. For more information, please follow other related articles on the PHP Chinese website!