Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Stream.mapMult of Java16

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to use the Stream.mapMult of Java16". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to use the Stream.mapMult of Java16" can help you solve the problem.

The following example shows how to replace each string in the stream with uppercase and lowercase versions of mapMulti:

Stream.of ("Twix", "Snickers", "Mars") .mapMulti (s, c)-> {c.accept (s.toUpperCase ()); c.accept (s.toLowerCase ());}) .forEach (System.out::println); Output:TWIXtwixSNICKERSsnickersMARSmars

The same thing can be done in a way like flaMap:

Stream.of ("Twix", "Snickers", "Mars") .flatMap (s-> Stream.of (s.toUpperCase (), s.toLowerCase () .forEach (System.out::println)

So what's the difference between mapMulti and flatMap? According to javadocs:

This method is preferable to flatMap in the following situations:

When each stream element is replaced with a small number of (possibly zero) elements. Using this approach avoids the overhead of creating a new Stream instance for each set of result elements, as required by flatMap.

When it is easier to generate result elements using an imperative method than to return them as a stream.

Examining the code of multiMap, we can see that it is delegated to flatMap, but it SpinedBuffer uses a to save the element before creating the flow, thus avoiding the overhead of creating a new stream for each set of result elements.

Default Stream mapMulti (BiConsumer

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report