Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In Reactive Programming, it’s often necessary to transform Collections into a reactive stream known as a Flux. This becomes a crucial step when integrating an existing data structure into the reactive pipeline.

In this tutorial, we’ll explore how to transform a Collection of elements into a Flux of elements.

2. Problem Definition

The two main types of Publishers in Project Reactor are Flux and Mono. Mono can emit at most one value, and Flux can emit an arbitrary number of values.

When we fetch a List<T>, we can either wrap it in a Mono<List<T>> or convert it to a Flux<T>. A blocking call that returns List<T> can be wrapped in a Mono, emitting the entire list in one big emit.

However, if we put such a large list in a Flux, it allows the Subscriber to request data in manageable chunks. This enables the subscriber to process items one by one or in small batches:

List to Flux Conversion

source: projectreactor.io

 

We’ll be exploring different approaches for converting a List that already holds elements of type T. For our use case, we’ll be considering the operators fromIterable and create of Publisher type Flux to transform a List<T> to a Flux<T>.

3. fromIterable

Let’s first create a List of type Integer and add some values to it:

List<Integer> list = List.of(1, 2, 3);

fromIterable is an operator on the Flux Publisher that emits the items contained in the provided collection.

We’ve used the log() operator to log each element published:

private <T> Flux<T> listToFluxUsingFromIterableOperator(List<T> list) {
    return Flux
      .fromIterable(list)
      .log();
}

Then we can apply the fromIterable operator to our Integer List and observe the behavior:

@Test
public void givenList_whenCallingFromIterableOperator_thenListItemsTransformedAsFluxAndEmitted(){

    List<Integer> list = List.of(1, 2, 3);
    Flux<Integer> flux = listToFluxUsingFromIterableOperator(list);

    StepVerifier.create(flux)
      .expectNext(1)
      .expectNext(2)
      .expectNext(3)
      .expectComplete()
      .verify();
}

Finally, we used the StepVerifier API to verify the emitted elements from the Flux with the elements that were in the List. After we wrap up our Flux source, which is under test, we use the expectNext method to cross-reference whether the items emitted from the Flux and items inside the List are identical and follow the same order.

4. create

The create operator on the Flux type Publisher enables us to create a Flux using the FluxSink API programmatically.

While the fromIterable is generally a good option for most cases, it’s not straightforward to use when the list is generated by a callback. In such cases, using the create operator is more suitable.

Let’s create an interface for a callback:

public interface Callback<T>  {
    void onTrigger(T element);
}

Next, let’s imagine a List<T> that’s returned from an asynchronous API call:

private void asynchronousApiCall(Callback<List<Integer>> callback) {
    Thread thread = new Thread(()-> {
      List<Integer> list = List.of(1, 2,3);
      callback.onTrigger(list);
    });
    thread.start();
}

Now, instead of fromIterable, let’s use FluxSink inside of the callback to add each of those items to the list:

@Test
public void givenList_whenCallingCreateOperator_thenListItemsTransformedAsFluxAndEmitted() {

    Flux<Integer> flux = Flux.create(sink -> {
      Callback<List<Integer>> callback = list -> {
        list.forEach(sink::next);
        sink.complete();
      };
      asynchronousApiCall(callback);
    });

    StepVerifier.create(flux)
      .expectNext(1)
      .expectNext(2)
      .expectNext(3)
      .expectComplete()
      .verify();
}

5. Conclusion

In this article, we explored different methods to transform a List<T> into a Flux<T> using the operators fromIterable and create in Publisher type Flux. The fromIterable operator can be used with type List<T> as well as with List<T> wrapped in a Mono.  The create operator is best suited for List<T> created from a callback.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW