• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Advantages of Stream#mapMulti ?

 
Sheriff
Posts: 4646
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java 16 added Stream#mapMulti.

It seems like one advantage of using this method over flatMap or a combination of filter and map is the reduction in the number of streams created; which probably translates into better performance.  Are there any other benefits?
 
Saloon Keeper
Posts: 15731
368
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The advantage of using mapMulti() over using filter() and map() is the same advantage that using flatMap() has: flatMap() and mapMulti() can both expand fewer elements into more elements, filter() can only reduce the number of elements.

For example, you can not use filter() and map() to expand a stream of countries into a stream of cities belonging to those countries.

So the comparison is really just between mapMulti() and flatMap(). Honestly, I can't say much more than what the Javadocs already say. Besides the performance benefit of avoiding a large number of streams, mapMulti() is useful if your mapping operation is more readable in imperative programming style.

Here is an example:

This example benefits doubly: Most countries have exactly one capital city, which means flatMap() would have used a high ratio of streams to capitals. This example is also more readable in imperative style. I'll leave it up to you to write a version that uses flatMap().

In practice, I hardly ever encounter a situation where I'm tempted to use mapMulti(). I mostly design my classes in such a way that a declarative style is favoured:

This doesn't get the performance benefit of mapMulti(), but in practice it doesn't matter.

If you can't or don't want to add a method capitals() to Country, then you might favour the mapMulti() approach.
 
Ron McLeod
Sheriff
Posts: 4646
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the example - it helped me realize that accept could be called more than once - that lets me avoid needing to collect downstream elements in a temporary collection when using flatMap.

For example - I have an annotation which defines what types of content a class can process:
In order to work-around some issues using Graalvm to create a native executable, I need to extract and transform to the information in annotations at Graalvm's build time.  For each annotation in, there could be multiple elements out.  In the example below, I collected the elements to a List, returned it as a stream, and then flattened it using flatMap:
Using mapMulti, I can eliminate the List:
 
Stephan van Hulst
Saloon Keeper
Posts: 15731
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no need to call Arrays.stream():
 
Marshal
Posts: 8965
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cowgratulations, Ron! Your topic has been published in our CodeRanch's February 2024 Edition Journal
 
Right! We're on it! Let's get to work tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic