Home  >  Article  >  Java  >  What is the difference between Optional.ifPresentOrElse() and Optional.or() methods in Java 9?

What is the difference between Optional.ifPresentOrElse() and Optional.or() methods in Java 9?

PHPz
PHPzforward
2023-08-26 20:49:02671browse

Java 9中Optional.ifPresentOrElse()和Optional.or()方法之间的区别是什么?

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete