How to manipulate the data inside Mono and Flux ?

Reactor is a Java library for creating reactive non-blocking applications on the JVM based on the Reactive Streams Specification.

This article is the second of a series which goal is to guide you through the process of creating, manipulating and managing the execution of the Reactive Streams that offer Reactor through the Mono and Flux classes.

In this second article, I’ll show you how values in Mono and Flux can be modified and transformed. We’ll do this through examples.

Applying mathematical operations on your Flux or Mono

Let’s say you want to compute the square of every integer value in the interval 1 to 100. To do this with a Flux, you first have to create a Flux containing all the integer values from 1 to 100, doing Flux.range(1, 100) .

Then, how do you compute the square of each value ?

Mono and Flux provide a map method which interface is Flux<B> map(Function<A, B> mapper) . This means that passing a function that transforms an element of type A to a type B to the map method, your Flux<A> will apply this function to each element in the Flux, which results in transforming your Flux<A> into a Flux<B>. Note that it is possible that B is the same type as A. Using this method, we can apply the square function on our Flux. Let me demonstrate:

 

This works the same with Mono.

Applying several transformations on Flux and Mono

Now, let’s take a look at an example that chains multiple map calls.

 

The flatMap method

The flatMap method is similar to the map method with the key difference that the supplier you provide to it should return a Mono<T> or Flux<T> . Using the map method would result in a Mono<Mono<T>> whereas using flatMap results in a Mono<T> .

For example, it is useful when you have to make a network call to retrieve a data, with a java api that returns a Mono, and then another network call that needs the result of the first one.

 

Also, it allows to handle errors precisely. Let me demonstrate with another example below.

 

The zip method

Let’s understand this method with an example. Imagine you want to write a method that retrieve the information of a user plus all the comments she wrote on your website.

Using a flatMap, you would write:

 

The problem with the above code is that it will make the call to get the user comments after your program received the userInfo. It’s not good at all for your users experience.

The zip method allows to easily combine the results of several Mono with the great benefit that the execution of your zip method will last as much as the longest Mono , not the sum of all the executions.

So, doing the same thing as above can now be done with the below code:

 

Filtering the elements of a Flux or Mono

You can also apply a filter on the elements that holds a Flux or a Mono with the filter method.

 

Selecting a subset of a Flux

While working with Flux, you can select a subset of the Flux. The class provides 7 methods to select the subset that best fits your needs. These methods are:

public Flux<T> take(long n)
public Flux<T> take(Duration timespan)
public Flux<T> take(Duration timespan, Scheduler timer)
public Flux<T> takeLast(int n)
public Flux<T> takeUntil(Predicate<? super T> predicate)
public Flux<T> takeUntilOther(Publisher<?> other)
public Flux<T> takeWhile(Predicate<? super T> continuePredicate)

By default, the take methods take the first elements of the Flux.

Conclusion

This second article guided you trough applying transformations on the elements of a Flux or a Mono.

It the next article, I’ll show you how Mono and Flux behave.

Article #3: https://medium.com/@cheron.antoine/reactor-java-3-how-do-mono-and-flux-behave-1b57ed1c432c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值