当前位置:网站首页>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);
}
边栏推荐
- NFT contract basic knowledge explanation
- Redis 迁移(操作流程建议)
- Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system
- Tweenmax+svg switch color animation scene
- Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl
- IAR工程适配GD32芯片
- 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍
- Solana扩容机制分析(2):牺牲可用性换取高效率的极端尝试 | CatcherVC Research
- 请指教同花顺软件究竟是什么?网上开户是否安全么?
- Practice of federal learning in Tencent micro vision advertising
猜你喜欢

牛客小白月赛50

Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system

清华“神奇药水”登Nature:逆转干细胞分化,比诺奖成果更进一步,网友:不靠精子卵子就能创造生命了?!...

了解下常见的函数式接口

Svg capital letter a animation JS effect

5000 word analysis: the way of container security attack and defense in actual combat scenarios

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

【力扣刷题】单调栈:84. 柱状图中最大的矩形

今年高考英语AI得分134,复旦武大校友这项研究有点意思
随机推荐
Ten thousand words! In depth analysis of the development trend of multi-party data collaborative application and privacy computing under the data security law
IntelliJ idea -- Method for formatting SQL files
基于 MATLAB的自然过渡配音处理方案探究
stm32h7b0替代h750程序导致单片机挂掉无法烧录程序问题
11 cnn简介
5 model saving and loading
JS教程之 使用 Electron.JS 构建原生桌面应用程序乒乓游戏
(DFS search) acwing 2005 horseshoe
Practice of federal learning in Tencent micro vision advertising
[time complexity and space complexity]
李飞飞团队将ViT用在机器人身上,规划推理最高提速512倍,还cue了何恺明的MAE...
精致妆容成露营“软实力”,唯品会户外美妆护肤产品销量激增
6 custom layer
【蓝桥杯集训100题】scratch辨别质数合数 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第15题
Svg animation around the earth JS special effects
R language generalized linear model function GLM, GLM function to build logistic regression model, analyze whether the model is over discrete, and use the ratio of residual deviation and residual degr
9 Tensorboard的使用
Solidus Labs欢迎香港前金融创新主管赵嘉丽担任战略顾问
Big talk Domain Driven Design -- presentation layer and others
Oilfield exploration problems