Home > Java > javaTutorial > 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
Release: 2023-08-26 20:49:02
forward
690 people have browsed it

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>
Copy after login

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())
   }
}
Copy after login

Output

<strong>1
0
0
1</strong>
Copy after login

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>
Copy after login

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

Output

<strong>Test1
Test2</strong>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template