liveBook · Manning

Flux.fromIterable()

As explained in Spring in Action, Sixth Edition

Flux.fromIterable() is a Project Reactor factory operation that creates a Flux from an Iterable source.

Flux.fromIterable() creates a Project Reactor Flux from a Java Iterable. Pass it a List, Set, or another Iterable when you have existing collection data but want to work with a Flux. section 11.3.1

Create and check a Flux

A list of fruit names can become a Flux<String> with Flux.fromIterable(fruitList). StepVerifier can then check that the Flux publishes the expected names and completes. section 11.3.1

@Test
public void createAFlux_fromIterable() {
  List<String> fruitList = new ArrayList<>();
  fruitList.add("Apple");
  fruitList.add("Orange");
  fruitList.add("Grape");
  fruitList.add("Banana");
  fruitList.add("Strawberry");
  
  Flux<String> fruitFlux = Flux.fromIterable(fruitList);
  
  StepVerifier.create(fruitFlux)
      .expectNext("Apple")
      .expectNext("Orange")
      .expectNext("Grape")
      .expectNext("Banana")
      .expectNext("Strawberry")
      .verifyComplete();
}

The complete test shows the source list, the factory call, and StepVerifier assertions together. section 11.3.1

The same conversion can bridge a conventional repository and a reactive controller: `Flux.fromIterable(tacoRepo.findAll()).take(12)` returns a Flux containing at most 12 tacos. This is a limited bridge, not an end-to-end reactive stack. A repository that already returns a Flux lets the controller use `tacoRepo.findAll().take(12)` without converting an Iterable. section 12.1.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Shows the factory call, a verification test, and a controller use; stops short of implementing a reactive repository.
Examples
Uses a fruit-list verification test and a taco controller that limits a converted repository result with take(12).
What it assumes
Assumes basic Java collection use; demonstrates Flux creation without requiring a reactive repository.
What you can do after
Apply it via Project Reactor to create a Flux from an Iterable and use Flux operations such as take().
How it connects
Links Java Iterable and List to Project Reactor Flux, StepVerifier, Spring WebFlux controllers, and reactive repositories.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.