首頁  >  文章  >  Java  >  Java 9中Optional.ifPresentOrElse()和Optional.or()方法之間的差異是什麼?

Java 9中Optional.ifPresentOrElse()和Optional.or()方法之間的差異是什麼?

PHPz
PHPz轉載
2023-08-26 20:49:02680瀏覽

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

在Java 9版本中,引入了Optional.ifPresentOrElse()Optional.or()方法來改進其功能。 Optional.ifPresentOrElse()方法檢查值是否存在,如果存在則應用具有值的操作,否則返回空操作,而Optional.or()方法檢查值是否存在,如果存在則傳回包含值的選項,否則傳回應用於Supplier函數的Optional。 Optional.ifPresentOrElse()方法包含兩個參數,ConsumerRunnable,而Optional.or()方法只包含一個參數,Supplier

Optional.ifPresentOrElse()的語法:

<strong>public void ifPresentOrElse(Consumer<? super T><!--? super T--> action, Runnable emptyAction)</strong>

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())
   }
}

輸出

<strong>1
0
0
1</strong>

#Optional.or()的語法:

<strong>public Optional<T> or(Supplier<? extends Optional<? extends T<!--? extends Optional<? extends T-->>> supplier)</strong>

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());
   }
}

輸出

<strong>Test1
Test2</strong>

以上是Java 9中Optional.ifPresentOrElse()和Optional.or()方法之間的差異是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除