


What is the difference between Optional.ifPresentOrElse() and Optional.or() methods in Java 9?
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
Optional.ifPresentOrElse(): The Chinese translation of
<strong>public void ifPresentOrElse(Consumer<? super T><!--? super T--> action, Runnable emptyAction)</strong>
Example
is:Example
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()) } }
Output
<strong>1 0 0 1</strong>
The syntax of Optional.or(): The Chinese translation of
<strong>public Optional<T> or(Supplier<? extends Optional<? extends T<!--? extends Optional<? extends T-->>> supplier)</strong>
Example
is:Example
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()); } }
Output
<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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.
