-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c55858
commit d7e89e7
Showing
7 changed files
with
163 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
在Java中,`Optional` 是一个容器类,表示值可能存在也可能不存在。它可以作为一个方法的返回结果,用来表示该方法可能没有返回任何内容。 | ||
这样可以避免使用null,并且可以更好地处理那些可能不存在的值。使用 `Optional` 可以帮助开发者更好地避免空指针异常。 | ||
|
||
### Optional 的基本用法: | ||
|
||
#### 创建 Optional 对象: | ||
可以通过 `Optional.of(value)` 或者 `Optional.ofNullable(value)` 方法来创建 `Optional` 对象。其中 `of` 要求 value 不为 null,而 `ofNullable` 则接受 null 参数。 | ||
|
||
```java | ||
Optional<String> optValue = Optional.of("Hello"); | ||
Optional<String> optNullValue = Optional.ofNullable(null); | ||
``` | ||
|
||
#### 检查值是否存在: | ||
可以使用 `isPresent()` 方法来检查是否有值存在。 | ||
|
||
``` java | ||
if (optValue.isPresent()) { | ||
System.out.println(optValue.get()); | ||
} | ||
``` | ||
|
||
#### 获取值: | ||
如果值存在,可以通过 `get()` 方法获取它;但如果值不存在,则会抛出 `NoSuchElementException` 异常。 | ||
|
||
```java | ||
String value = optValue.get(); | ||
``` | ||
|
||
为了避免这种异常,通常会结合 `isPresent()` 方法使用或者使用 `orElse()` 或 `orElseGet()` 方法来提供一个默认值。 | ||
|
||
```java | ||
String defaultValue = optNullValue.orElse("Default Value"); | ||
``` | ||
|
||
#### 使用 `orElseGet()` 方法: | ||
`orElseGet()` 方法与 `orElse()` 类似,但是它允许延迟加载默认值。 | ||
|
||
``` java | ||
String defaultValueLazy = optNullValue.orElseGet(() -> "Computed Default Value"); | ||
``` | ||
|
||
#### 使用 `ifPresent()` 方法: | ||
这个方法允许你传入一个 `Consumer`,当 `Optional` 包含值时执行某些操作。 | ||
|
||
``` java | ||
optValue.ifPresent(System.out::println); | ||
``` | ||
|
||
#### 使用 `map()` 和 `flatMap()` 方法: | ||
这些方法允许对 `Optional` 中的值进行转换。 | ||
|
||
```java | ||
Optional<Integer> length = optValue.map(String::length); | ||
``` | ||
|
||
`flatMap` 则是用于将当前 `Optional` 映射到另一个 `Optional`。 | ||
|
||
```java | ||
Optional<Integer> lengthFlat = optValue.flatMap(str -> Optional.of(str.length())); | ||
``` | ||
|
||
### 注意事项: | ||
虽然 `Optional` 是一个很有用的功能,但它并不总是适合所有情况。例如,在需要返回多个值或错误信息的情况下,可能需要考虑其他设计模式或数据结构。此外,过度使用 `Optional` 可能会导致代码变得难以阅读和维护。因此,应当谨慎地选择何时以及如何使用 `Optional`。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
在Java中,`Stream` API提供了一种强大的方式来处理集合数据。`reduce`方法是`Stream` API中的一个重要方法,它用于将流中的元素组合起来,生成一个单一的结果。 | ||
`reduce`方法可以接受两种形式的参数: | ||
|
||
1. **一个身份元素(identity value)** 和 **一个归约操作(reduction operation)**: | ||
``` java | ||
Optional<U> reduce(U identity, BinaryOperator<U> accumulator) | ||
``` | ||
这个形式会返回一个`Optional`对象,包含了通过使用给定的`BinaryOperator`来组合流中元素得到的结果。如果流是空的,则返回 | ||
`Optional.empty()`。 | ||
|
||
2. **仅仅一个归约操作**: | ||
``` java | ||
U reduce(BinaryOperator<U> accumulator) | ||
``` | ||
这个形式没有提供默认值或身份元素,并且如果流为空,则抛出`NoSuchElementException`异常。 | ||
|
||
``` java | ||
public class ReduceDemo { | ||
public static void main(String[] args) { | ||
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); | ||
|
||
Integer reduce = list.stream().reduce(3, Integer::sum); | ||
System.out.println("reduce = " + reduce); | ||
|
||
Optional<Integer> reduce1 = list.stream().reduce((a, b) -> a * b); | ||
System.out.println("reduce1 = " + reduce1); | ||
|
||
Integer reduce2 = list.stream().reduce(3, (a, b) -> a * b); | ||
System.out.println("reduce2 = " + reduce2); | ||
} | ||
} | ||
``` | ||
|
||
在这个例子中,我们首先创建了一个包含一些整数的列表。然后,我们使用`reduce` | ||
方法计算了这些数的总和,并且还计算了它们的乘积。注意,在计算乘积的时候,因为我们没有提供初始值,所以使用了`Optional` | ||
来处理可能的空流情况。 | ||
|
||
`reduce`方法的一个关键特性是它能够支持并行流的处理,因为它的操作是无副作用并且可以结合的,这使得它非常适合于并行执行。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
Guava的`EventBus`是一种发布/订阅的消息传递框架,它简化了应用程序中不同组件之间的通信。它允许解耦的应用程序组件通过发送事件来与其他组件通信,而不需要知道谁或者是否有人会处理这些事件。这有助于提高代码的可维护性和可测试性。 | ||
|
||
下面是一个简单的使用`EventBus`的例子: | ||
|
||
1. **定义事件类** - 首先你需要定义一个事件类,该类通常包含一些数据,这些数据将由监听该事件的订阅者处理。 | ||
|
||
```java | ||
public static class SimpleEvent { | ||
private final String message; | ||
public SimpleEvent(String message) { this.message = message; } | ||
public String getMessage() { return message; } | ||
} | ||
``` | ||
|
||
2. **创建EventBus实例** - `EventBus`是单例模式的,你可以直接创建一个新的`EventBus`实例。 | ||
|
||
``` java | ||
EventBus eventBus = new EventBus(); | ||
``` | ||
|
||
3. **注册订阅者** - 接下来需要注册一个或多个订阅者(listener)。订阅者通常实现了一个方法,该方法会在接收到事件时被调用。 | ||
|
||
``` java | ||
class SimpleEventListener { | ||
@Subscribe | ||
public void handleSimpleEvent(SimpleEvent event) { | ||
System.out.println("Received message: " + event.getMessage()); | ||
} | ||
} | ||
|
||
SimpleEventListener listener = new SimpleEventListener(); | ||
eventBus.register(listener); | ||
``` | ||
|
||
4. **发布事件** - 当需要通知订阅者时,可以向`EventBus`发布事件。 | ||
|
||
``` java | ||
eventBus.post(new SimpleEvent("Hello EventBus!")); | ||
``` | ||
|
||
5. **注销订阅者** - 当不再需要接收事件时,可以注销订阅者以避免内存泄漏。 | ||
|
||
``` java | ||
eventBus.unregister(listener); | ||
``` | ||
请注意,以上示例仅用于说明目的,并且在实际应用中可能需要根据具体情况进行调整。此外,`EventBus`也支持异步事件处理,允许在一个单独的线程中执行事件处理逻辑,这对于需要长时间运行的任务或者可能导致UI阻塞的任务特别有用。 | ||
从Guava 30版本开始,`EventBus`已经被标记为`@DoNotUse`,并且在Guava 31中已被移除,因为其设计存在一些问题,特别是关于异常处理方面的问题。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
[ | ||
"docs/java/java8/Optional.md", | ||
"docs/java/java8/stream/reduce.md", | ||
"docs/java/utils/guava/EventBus.md", | ||
"temp.md", | ||
"docs/java/framework/spring/annotation/Async.md", | ||
"docs/java/framework/spring/annotation/DependsOn.md", | ||
"docs/java/framework/spring/annotation/PostConstruct.md", | ||
"docs/java/framework/spring/annotation/annotation.md", | ||
"docs/java/utils/mapStruct/index.md", | ||
"docs/business/01signup.md", | ||
"docs/business/index.md", | ||
"docs/database/minio/index.md", | ||
"docs/{objectStorage => database}/minio/java.md", | ||
"docs/java/utils/guava/index.md" | ||
"docs/java/framework/spring/annotation/index.md", | ||
"docs/java/framework/spring/index.md" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
接口的幂等性(Idempotence)是指一个HTTP请求(或任何其他类型的请求)可以多次执行相同的效果而不改变结果。 | ||
换句话说,如果一个操作是幂等的, 那么无论该操作被执行多少次,从第一次成功执行后的结果都将是相同的。 | ||
换句话说,如果一个操作是幂等的, 那么无论该操作被执行多少次,从第一次成功执行后的结果都将是相同的。 | ||
|
||
幂等操作是指可以在任意次数上重复执行的操作,并且每次执行都会产生相同的效果,不会改变系统的最终状态 | ||
|