当前位置:网站首页>Mono 的一些实例方法
Mono 的一些实例方法
2022-06-26 16:02:00 【_lrs】
1.as
@Test
public void as() {
//传入 Function<? super Mono<T>, P> ,对 mono 进行中间的映射转换成 P
Mono.just("hello mono").as(v -> v.subscribe(System.out::println));
}
2.map
@Test
public void map() {
//对元素进行映射
Mono.just("hello mono").map(v -> v + " map").subscribe(System.out::println);
}
@Test
public void mapNotNull() {
//对元素进行映射,元素不能是null
Mono.just("hello mono").mapNotNull(v -> v + " map").subscribe(System.out::println);
}
3.flatMap
@Test
public void flatMap() {
//对元素进行映射,返回值经过 Mono 包装
Mono.just("hello mono").flatMap(v -> Mono.just(v).map(item -> item + "map")).subscribe(System.out::println);
}
@Test
public void flatMapMany() {
//对元素进行映射,返回值经过 Publisher 包装
Mono.just("hello mono").flatMapMany(v -> Mono.just(v).map(item -> item + "map")).subscribe(System.out::println);
}
@Test
public void flatMapMany2() {
//对元素进行映射,第一个参数返回值经过 Publisher 包装,第二个参数 在出错时调用,第三个参数 在正常结束时调用
Mono.just("hello mono")
.flatMapMany(v -> Mono.just(v).map(item -> item + "map"),
error -> Mono.just(error.getMessage()),
() -> Mono.just("...end..."))
.subscribe(System.out::println);
}
@Test
public void flatMapIterable() {
//将元素进行映射成 Iterable
Mono.just("hello mono").flatMapIterable(v -> Arrays.asList(v.split(" "))).subscribe(System.out::println);
}
4.fliter
@Test
public void filter() {
//将元素进行过滤
Mono.just("hello mono").filter(v -> v != null).subscribe(System.out::println);
}
@Test
public void filterWhen() {
//将元素通过 Publisher<Boolean>> 进行过滤
Mono.just("hello mono").filterWhen(v -> {
if (v != null) {
return Mono.just(true);
}
return Mono.just(false);
}).subscribe(System.out::println);
}
5.then
@Test
public void then() {
//输出then里的mono
Mono.just("hello mono").then(Mono.just("then")).subscribe(System.out::println);
}
@Test
public void thenEmpty() {
//返回空的 Mono
Mono.just("hello mono").thenEmpty(Mono.empty()).subscribe(System.out::println);
}
@Test
public void thenMany() {
//返回 Flux
Mono.just("hello mono").thenMany(Mono.just("thenMany")).subscribe(System.out::println);
}
@Test
public void thenReturn() {
//传入值
Mono.just("hello mono").thenReturn("thenReturn").subscribe(System.out::println);
}
6.defaultIfEmpty
@Test
public void defaultIfEmpty() {
//为空时的默认值
Mono.empty().defaultIfEmpty("defaultIfEmpty").subscribe(System.out::println);
}
7.switchIfEmpty
@Test
public void switchIfEmpty() {
//为空时的默认值,传入mono
Mono.empty().switchIfEmpty(Mono.just("switchIfEmpty")).subscribe(System.out::println);
}
8.do
@Test
public void doTest() {
//建立订阅关系时执行doFirst
Mono.just("hello mono").doFirst(() -> System.out.println("doFirst")).subscribe(System.out::println);
//消费元素之后执行倒序执行doAfterTerminate
Mono.just("hello mono")
.doAfterTerminate(() -> System.out.println("1"))
.map(v -> v + " map")
.doAfterTerminate(() -> System.out.println("2"))
.subscribe(System.out::println);
//消费元素之后,在onComplete 之后执行doFinally
Mono.just("hello mono").doFinally(t -> System.out.println(t.toString())).subscribe(System.out::println);
//消费元素之后执行doOnSuccess,在doAfterTerminate之前执行
Mono.just("hello mono").doOnSuccess(System.out::println).subscribe(System.out::println);
//订阅关系调用cacel()时回调
Mono.just("hello mono").doOnCancel(System.out::println).subscribe(System.out::println);
//建立订阅关系调用 onSubscribe() 时回调
Mono.just("hello mono").doOnSubscribe(System.out::println).subscribe(System.out::println);
//订阅关系调用request()时回调
Mono.just("hello mono").doOnRequest(System.out::println).subscribe(System.out::println);
//订阅者调用onNext()时回调
Mono.just("hello mono").doOnNext(System.out::println).subscribe(System.out::println);
//订阅者调用onNext()时回调
Mono.just("hello mono").doOnEach(System.out::println).subscribe(System.out::println);
//订阅关系是 CancelledSubscription 时回调
Mono.just("hello mono").doOnDiscard(String.class, System.out::println).subscribe(System.out::println);
//订阅者调用onError()时回调
Mono.defer(() -> Mono.just(1 / 0)).doOnError(System.out::println).subscribe(System.out::println);
}
9.on
@Test
public void onTest() {
//onNext() 出错的时候回调
Mono.just("hello mono")
.map(v -> 1 / 0)
.onErrorContinue((error, obj) -> System.out.println(obj + ":" + error))
.subscribe(System.out::println);
//调用 onError() 时对异常进行映射
Mono.just("hello mono")
.map(v -> 1 / 0)
.onErrorMap(error -> error)
.subscribe(System.out::println);
//异常的时候回调onErrorResume获取mono作为发布者
Mono.just("hello mono")
.map(v -> 1 / 0)
.onErrorResume(error -> Mono.just(1))
.subscribe(System.out::println);
//异常的时候回调onErrorReturn获取值 被mono包装后作为发布者
Mono.just("hello mono")
.map(v -> 1 / 0)
.onErrorReturn(1)
.subscribe(System.out::println);
//异常时不处理异常
Mono.just("hello mono")
.map(v -> 1 / 0)
.onErrorStop()
.subscribe(System.out::println);
//异常时断开订阅关系
Mono.just("hello mono")
.map(v -> 1 / 0)
.onTerminateDetach()
.subscribe(System.out::println);
}
边栏推荐
- IntelliJ idea -- Method for formatting SQL files
- 6 自定义层
- 理想路径问题
- 7 user defined loss function
- H5 close the current page, including wechat browser (with source code)
- Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's Mae
- 面试踩坑总结一
- 简单科普Ethereum的Transaction Input Data
- R语言plotly可视化:plotly可视化归一化的直方图(historgram)并在直方图中添加密度曲线kde、并在直方图的底部边缘使用geom_rug函数添加边缘轴须图
- How to create your own NFT (polygon) on opensea
猜你喜欢

Exquisite makeup has become the "soft power" of camping, and the sales of vipshop outdoor beauty and skin care products have surged

The first batch in the industry! Tencent cloud security and privacy computing products based on angel powerfl passed CFCA evaluation

Stepn débutant et avancé

JS text scrolling scattered animation JS special effect

Ten thousand words! In depth analysis of the development trend of multi-party data collaborative application and privacy computing under the data security law

Development, deployment and online process of NFT project (1)

This year, the AI score of college entrance examination English is 134. The research of Fudan Wuda alumni is interesting

2 three modeling methods

How to identify contractual issues

牛客小白月赛50
随机推荐
11 cnn简介
Solidus Labs欢迎香港前金融创新主管赵嘉丽担任战略顾问
Redis 迁移(操作流程建议)1
Redis的ACID
若依如何实现接口限流?
首例猪心移植细节全面披露:患者体内发现人类疱疹病毒,死后心脏重量翻倍,心肌细胞纤维化丨团队最新论文...
Svg rising Color Bubble animation
[time complexity and space complexity]
JVM notes
Keepalived 实现 Redis AutoFailover (RedisHA)
Redis 迁移(操作流程建议)
3. Keras version model training
Comprehensive analysis of discord security issues
Transaction input data of Ethereum
NFT 项目的开发、部署、上线的流程(1)
Redis顺序排序命令
Redis order sorting command
NFT合约基础知识讲解
牛客小白月赛50
油田勘探问题