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 merge Stream streams in Java

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail how to merge Stream streams in Java, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1. The preface Java Stream Api [1] provides a lot of useful Api that makes it easy to convert a collection or multiple elements of the same type into a stream. Today we'll look at how to merge Stream streams. 2. Merging of Stream streams the premise of merging Stream streams is that the types of elements are consistent. 2.1 the easiest way for concat to merge streams is through the Stream.concat () static method: Stream stream = Stream.of (1,2,3)

Stream another = Stream.of (4,5,6)

Stream concat = Stream.concat (stream, another)

List collect = concat.collect (Collectors.toList ())

List expected = Lists.list (1,2,3,4,5,6)

Assertions.assertIterableEquals (expected, collect)

This kind of merge is to splice the two streams one before and after the other:

2.2 merging of multiple streams We can also use the above method to "nesting dolls": Stream.concat (Stream.concat (stream, another), more)

You can go on layer by layer, and if you need to merge more streams, it doesn't look very clear. I have previously introduced a flatmap operation of Stream [2], and its general flow can be seen in this diagram: so we can merge multiple streams through flatmap: Stream stream = Stream.of (1, 2, 3)

Stream another = Stream.of (4,5,6)

Stream third = Stream.of (7,8,9)

Stream more = Stream.of (0)

Stream concat = Stream.of (stream,another,third,more).

FlatMap (integerStream-> integerStream)

List collect = concat.collect (Collectors.toList ())

List expected = Lists.list (1,2,3,4,5,6,7,8,9,0)

Assertions.assertIterableEquals (expected, collect)

This approach is to first generate a stream of type Stream by using multiple streams as elements, and then merge them by flatmap tiling. 2.3 third-party libraries have many third-party enhanced libraries such as StreamEx and Joo λ that can be merged. In addition, the reactive programming library Reactor 3 [3] can also combine Stream streams into reactive streams, which may be useful in some scenarios. Here is a demonstration: List block = Flux.fromStream (stream) .mergeWith (Flux.fromStream (another)) .uplotList () .block ()

About how to merge stream streams in Java to share here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.

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: 211

*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

Internet Technology

Wechat

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

12
Report