liveBook · Manning

collectList

As explained in Spring in Action, Sixth Edition

collectList is a Project Reactor operator that collects all source elements into a List emitted by a Mono.

collectList() gathers every element emitted by a Flux and returns a Mono containing those elements in a List. section 11.3.3

Collecting fruit

Flux.just("apple", "orange", "banana", "kiwi", "strawberry") produces a Mono<List<String>> through collectList(). StepVerifier checks that the Mono emits the complete fruit list and then completes. section 11.3.3

Use collectList() when the entire stream should become one collection:

  • It emits one List from the source Flux through a Mono, unlike buffer(), which produces a Flux of lists. section 11.3.3
  • Use collectMap() instead when elements should be gathered into a Map using calculated keys. section 11.3.3
Figure 11.19 The collectList() operation results in a Mono containing a list of all messages emitted by the incoming Flux.

Shows the resulting Mono containing a List of all messages emitted by the incoming Flux. section 11.3.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines the Flux-to-Mono conversion and demonstrates it with a complete Java test, without detailing operator internals.
Examples
Uses a fruit Flux containing apple, orange, banana, kiwi, and strawberry, then verifies the resulting List.
What you can do after
Apply it via collectList() and verify the resulting Mono<List<String>> with StepVerifier.
How it connects
Connects Flux, Mono, buffer(), collectMap(), and StepVerifier through reactive stream collection and verification.

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.