"Optional<ButtonType>.get()" 和 "alert.getResult()" 之间的区别

Difference between "Optional<ButtonType>.get()" and "alert.getResult()"

我正在摆弄 JavaFX 并访问了一个网页,其中列出了所有不同类型的对话框,内容的创建者使用了如下内容:

Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Look, a Confirmation Dialog");
alert.setContentText("Are you ok with this?");

Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
    // ... user chose OK
} else {
    // ... user chose CANCEL or closed the dialog
}

我所做的是:

alert.showAndWait();
if (alert.getResult() == ButtonType.OK)
{
    //User chose OK
} 

else
{
    //User chose other
}

我阅读了大多数这些方法的文档 类 我可以从中了解到他使用 Optional 以防方法 showAndWait() return一个null。根据文档,showAndWait() 方法 return 是一个 Optional.ofNullable,可能是也可能不是 null。然后在 if 语句中,他从 Optional<ButtonType> 中获取结果,其中包含任何 showAndWait() returned 并进行比较。

在我的代码部分,我直接从 Alert 实例和 getResult() 获取结果并比较它可能是坏的,因为 showAndWait() 可能 return null.

这是主要区别吗?防止空值?还是有其他原因说明为什么使用 Optional 可能比这更好?只是想知道是否有人可以更好地解释这一点。

extra:另外,如果有人能告诉我 showAndWait() 的 return 类型是什么意思,我将不胜感激,它是 returns : Optional<R>。我假设它是一个通用类型,但我以前从未见过,我只见过 E T N

感谢您对此的任何意见。

这是主要区别吗?

没有。请参阅 Dialog

的 javadoc 中的以下评论

Shown below is three code snippets, showing three equally valid ways of showing a dialog:
Option 1: The 'traditional' approach

 Optional<ButtonType> result = dialog.showAndWait();
 if (result.isPresent() && result.get() == ButtonType.OK) {
     formatSystem();
 }

Option 2: The traditional + Optional approach

 dialog.showAndWait().ifPresent(response -> {
     if (response == ButtonType.OK) {
         formatSystem();
     }
 });

Option 3: The fully lambda approach

 dialog.showAndWait()
      .filter(response -> response == ButtonType.OK)
      .ifPresent(response -> formatSystem());

There is no better or worse option of the three listed above, so developers are encouraged to work to their own style preferences. The purpose of showing the above is to help introduce developers to the Optional API, which is new in Java 8 and may be foreign to many developers.

防止空值?

保护存在于 isPresent()ifPresent() 的所有选项中。

或者还有其他原因可以说明为什么使用 Optional 可能比这更好吗?

没有理由。 Optional was introduced with Java 8—the guys at Oracle want the developers to get familiar with it soon. Although the same feature was already introduced to "Java World" with 3rd-party APIs, now it is part of the JDK itself. You may read its advantages etc. on online blogs, for example Tired of Null Pointer Exceptions?.

此外,如果有人能告诉我 return 类型的 showAndWait() 是什么意思,我将不胜感激,它 returns : Optional<R>

R 是一个 类型参数 (或类型变量), "R" 代表“R 结果”。它可以是任何东西(XRERezult 等),只要不与保留的 Java 关键字或 class 中的 class 名称冲突]小路;通常它是一个大写字母:

Java Tutorials > Generics > Generic Types:

Type Parameter Naming Conventions

By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.

The most commonly used type parameter names are:

  • E - Element (used extensively by the Java Collections Framework)
  • K - Key
  • N - Number
  • T - Type
  • V - Value
  • S,U,V etc. - 2nd, 3rd, 4th types

You'll see these names used throughout the Java SE API and the rest of this lesson.