The collectMap operator

Just like collectlist(), Reactor also provides collectMap() to accumulate data into a java.util.Map; collectMap takes a key generator function to create keys for the generated value elements. This is shown in the following code:

fibonacciGenerator.take(10)
.collectMap(t -> t%2==0 ? "even": "odd")
.subscribe(t -> {
   System.out.println(t);
});

The preceding code generates a Map with two keys that are represented as even and odd. It will keep the last even/odd number in the map. This is shown as follows:

The collectMap command not only takes a keyGeneator, but also provides the option to pass a value generator. The value ...

Get Hands-On Reactive Programming with Reactor now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.