28-10-2021

We continue the series whose main character is reactive programming. For our further deliberations it will be important to repeat some regularities connected with it.

As we already know, Reactive Programming is built around stream processing. Data is handled as a stream of data points passing through a pipeline of functions or operators. Each operator can transform and manipulate the data before it is passed to the next operator.

Transforming streams

Operators can be a map, a collection, but also other elements. These operations are chained until the transformed data is returned. 

It is clear that writing a program that will work and be useful requires us to be able to work with reactive streams. This work, in turn, will not be effective, even impossible if we do not know how to create or transform streams. Today we’re just talking about the most common methods of creating and transforming them. 

Flux<T>.map()

map programowanie reaktywne

The map here is the same as the java 8 stream map, used to convert elements.

It is used for synchronous, non-blocking 1-to-1 transformations

For example. If you have a list of String and want to convert them all to uppercase, how would you do it? You would have to go through the List using a for or foreach loop and convert each element.

Using map() , you can apply any function to each element. It can be any predefined function or a user-defined function. You can use not only lambda expressions but also method references.

One example is converting a list of integers and then the square of each number. The map function is also an indirect operation and returns the stream of the transformed element.

Flux<T>.flat 

flat programowanie reaktywne class=

Flatmap is also an element conversion, but the difference is that flatmap converts elements to streams and then combines the streams into one large stream.

Applies to asynchronous (non-blocking) 1-to-N transformations

For each <T> input element, flatMap maps it to Publisher<V>.  The general case is that a Publisher may emit multiple elements.

The flatMap method is similar to the map method with the key difference that the provider you supply it with should return a Mono<T>or Flux<T>.

Flux<T>.filter()

filter

The reactor filter is the same as the java 8 stream filter.

It goes through all the elements present and removes or filters out all those elements that do not match the specified condition with a meaningful argument. It is basically an operation that takes place between the stream interface. The function returns an output stream containing elements of the input stream that match the given conditions.

Flux<T>.zip()

zip programowanie reaktywne

This operation may seem unfamiliar. As the name implies, „compression” involves combining multiple streams one-to-one.

There is another reason, because in each flux or mono stream, the speed of each stream is different. The zip function is also used to synchronize two streams. For example, we add another stream here. This stream will continuously emit elements. The speed of the other stream is limited to emit one element every second. That the combined stream will also be sent to the other stream. This results in a stream alignment.

Leave a Comment

Newsletter