Skip to content

修正 Java 10 中的 Optional 增强描述 #2691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/java/new-features/java10.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ list.stream().collect(Collectors.toUnmodifiableSet());

## Optional 增强

`Optional` 新增了`orElseThrow()`方法来在没有值时抛出指定的异常
`Optional` 新增了一个无参的 `orElseThrow()` 方法,作为带参数的 `orElseThrow(Supplier<? extends X> exceptionSupplier)` 的简化版本,在没有值时默认抛出一个 NoSuchElementException 异常

```java
Optional.ofNullable(cache.getIfPresent(key))
.orElseThrow(() -> new PrestoException(NOT_FOUND, "Missing entry found for key: " + key));
Optional<String> optional = Optional.empty();
String result = optional.orElseThrow();
```

## 应用程序类数据共享(扩展 CDS 功能)
Expand Down