当前位置:网站首页>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);
}
边栏推荐
- 简单科普Ethereum的Transaction Input Data
- Cookie和Session详解
- OpenSea上如何创建自己的NFT(Polygon)
- Beijing University and Tencent jointly build angel4.0, and the self-developed in-depth learning framework "River map" is integrated into the ecology
- SAP OData 开发教程 - 从入门到提高(包含 SEGW, RAP 和 CDP)
- JS教程之 使用 Electron.JS 构建原生桌面应用程序乒乓游戏
- 11 cnn简介
- 精致妆容成露营“软实力”,唯品会户外美妆护肤产品销量激增
- Panoramic analysis of upstream, middle and downstream industrial chain of "dry goods" NFT
- 首例猪心移植细节全面披露:患者体内发现人类疱疹病毒,死后心脏重量翻倍,心肌细胞纤维化丨团队最新论文...
猜你喜欢

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

1 张量的简单使用

11 cnn简介

Net基于girdview控件实现删除与编辑行数据

Angel 3.2.0 new version released! Figure the computing power is strengthened again

Arduino UNO + DS1302简单获取时间并串口打印

补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍

Comprehensive analysis of discord security issues

今年高考英语AI得分134,复旦武大校友这项研究有点意思

【蓝桥杯集训100题】scratch辨别质数合数 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第15题
随机推荐
SAP OData development tutorial - from getting started to improving (including segw, rap and CDP)
若依打包如何分离jar包和资源文件?
2 three modeling methods
「干货」NFT 上中下游产业链全景分析
Net based on girdview control to delete and edit row data
What is the process of switching C # read / write files from user mode to kernel mode?
NFT transaction principle analysis (2)
[untitled]
redis的二进制数组命令
C. Inversion Graph
股票开户优惠链接,我如何才能得到?在线开户安全么?
7 user defined loss function
Supplement the short board - Open Source im project openim about initialization / login / friend interface document introduction
Transformation of zero knowledge QAP problem
Solidus Labs欢迎香港前金融创新主管赵嘉丽担任战略顾问
6 自定义层
The first batch in the industry! Tencent cloud security and privacy computing products based on angel powerfl passed CFCA evaluation
Beijing University and Tencent jointly build angel4.0, and the self-developed in-depth learning framework "River map" is integrated into the ecology
Tsinghua's "magic potion" is published in nature: reversing stem cell differentiation, and the achievements of the Nobel Prize go further. Netizen: life can be created without sperm and eggs
心情不好,我就这样写代码