The source code is for testing only and should not be used commercially. The source code comes from the Internet. If there is any infringement, please contact me to remove it.
Java Stream 流的合并操作

1. preface

Java Stream Api It provides many useful Apis that make it convenient for us to convert collections or multiple elements of the same type into streams for operation. Today we will look at how to merge Stream flow.

2. Merging of Stream streams

Stream The prerequisite for stream merging is that the types of elements can be consistent.

2.1 concat

The easiest way to merge streams is by Stream.concat() Static method:

Stream<Integer> stream = Stream.of(1, 2, 3);
Stream<Integer> another = Stream.of(4, 5, 6);
Stream<Integer> concat = Stream.concat(stream, another);

List<Integer> collect = concat.collect(Collectors.toList());
List<Integer> expected = Lists.list(1, 2, 3, 4, 5, 6);

Assertions.assertIterableEquals(expected, collect);

This merge is to splice two streams one after the other:

2.2 Merging of multiple streams

For the merger of multiple streams, we can also use the above method to perform the "doll operation":

Stream.concat(Stream.concat(stream, another), more);

You can continue to cover it layer by layer. If there are too many streams that need to be merged, it won't look very clear.

I introduced one beforeflatmap operation of Stream , for its general process, you can refer to this picture inside:

So we can go through flatmap Implement and merge multiple streams:

Stream<Integer> stream = Stream.of(1, 2, 3);
Stream<Integer> another = Stream.of(4, 5, 6);
Stream<Integer> third = Stream.of(7, 8, 9);
Stream<Integer> more = Stream.of(0);
Stream<Integer> concat = Stream.of(stream,another,third,more).
    flatMap(integerStream -> integerStream);
List<Integer> collect = concat.collect(Collectors.toList());
List<Integer> expected = Lists.list(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
Assertions.assertIterableEquals(expected, collect);

This method is to first generate multiple streams as elements to generate a type of Stream<Stream<T>> of the flow, and then proceed flatmap Tiling operations merge.

2.3 third-party libraries

There are many third-party enhanced libraries StreamExJooλ Merge operations can be performed. In addition, reactive programming libraries Reactor 3 may be Stream Merging streams into reactive streams may be useful in some scenarios. Here's a demonstration:

List<Integer> block = Flux.fromStream(stream)
                       .mergeWith(Flux.fromStream(another))
                                 .collectList()
                                 .block();

3. summary

if you regularly use Java Stream Api , merge Stream Flow is a frequently encountered operation. Today we briefly introduced the merger Stream Flow method, I hope it will be useful to you.

read more
Resource download
PriceFree
The use is limited to testing, experiments, and research purposes. It is prohibited for all commercial operations. This team is not responsible for any illegal behavior of users during use. Please self-test all source codes! There is no guarantee of the integrity and validity of your source code. All source code is collected from the entire network
Original link:https://bcbccb.cn/en/4613.html, please indicate the source for reprinting. Disclaimer: This resource has not been authorized by the original rights holder and is not commercially available. It can only be used to learn and analyze the underlying code, CSS, etc., and is prohibited for commercial purposes. Any relevant disputes and legal liabilities arising from unauthorized commercial use shall be fully borne by the user. Everyone is responsible to support genuine copies. Please delete them within 24 hours after downloading. Thank you for your support!
1

Comments0

Cocos系列情怀源码多套精美UI界面皮肤切换全国600子游戏(带控制)
Cocos series emotions source code, multiple sets of exquisite UI interfaces, skin switching across 600 sub-games across the country (with control)
Someone just purchased Go and have a look

Site Announcements

The source code (theme/plug-in/application source code) and other resources provided by this site are only for learning and exchange

Commercial use is prohibited, otherwise all consequences will be borne by the downloading user!

Some resources are collected or copied online. If they infringe on your legitimate rights and interests, please write to us.

Currently, members have a big reward, and the current price for a lifetime member is 299 gold coins.Recent price adjustments

Join quickly, opportunities wait for no one! immediately participated in

Captcha

Fast login to social accounts

en_USEnglish