The flatMap operator

The preceding transformation example that used the map() operator was effective when we had a one-to-one value conversion, but it could not handle a one-to-n value conversion. We can show this premise by generating a stream of factors for our Fibonacci numbers. Let's first revise what factorization is.

In number theory, factorization is the breakup of a composite number into a product of smaller numbers. For 6, for example, the factors are 1, 2, 3, and 6.

Let's try to convert Fibonacci numbers to their corresponding factors. Each number of the series must be converted to all possible factors. First, let's build a simple function to compute factors:

class Factorization {
    Collection<Integer> findfactor(int number) {

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.