liveBook · Manning

collectMap

As explained in Spring in Action, Sixth Edition

collectMap is a Project Reactor operator that collects source elements into a Map whose keys are computed by a function and whose values are the elements.

collectMap collects every element emitted by a Flux into a Mono containing a Map. A supplied function computes each key, while the element becomes its value. section 11.3.3

Group animal names by first letter

A Flux containing "aardvark", "elephant", "koala", "eagle", and "kangaroo" becomes a Mono<Map<Character, String>> with collectMap(a -> a.charAt(0)). The final entries are a→aardvark, e→eagle, and k→kangaroo. When keys repeat, the last element replaces the earlier entry.

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines the operator and demonstrates its result with a StepVerifier test, including duplicate-key replacement.
Examples
Uses animal names keyed by their first characters, including repeated initials that demonstrate last-entry replacement.
What you can do after
Apply it via Project Reactor to produce a Mono containing a Map keyed by a computed element characteristic.
How it connects
Connects Flux, Mono, Map, Function, and collectList through reactive stream transformation and collection.

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.