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.
Java8 Stream Api 中的skip和limit操作

1. preface

Java 8 Stream API ofskip()andlimit()Methods have a similar effect. They are both intermediate methods of clipping flows. Today we will discuss these two methods.

2. skip()

skip(lang n) It's a pre-skip n Intermediate flow operations of each element. We wrote a simple method to do thisskipOperate to print out the remaining elements of the flow.

  public static void skip(long n) {
        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6);
        integerStream.skip(n).forEach(integer -> System.out.println("integer = " + integer));
    }

After testing, when n < 0 When it was directly thrown out IllegalArgumentException abnormal. After all, you have to skip some elements. Just like playing chess, you can't retreat but move forward. when n=0 At that time, he returned it intact. Generally, we will not take the initiative to do this kind of operation and it is meaningless. when n=4 When, printed 5 and 6 , we can infer that when we value the value greater than or equal to the size of the stream, there must be nothing left, and it must be an empty stream for the stream. After testing, it was confirmedcount=0

that is skip(long n) Method before skipping n (non-negative) elements, returning the remaining stream, which may be empty.

3. limit()

limit(long maxsize) We did the same:

    public static void limit(long maxsize) {
        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6);
         integerStream.limit( maxsize).forEach(integer -> System.out.println("integer = " + integer));
    }

when n < 0 When it was directly thrown out IllegalArgumentException abnormal. when n=0 When, an empty stream is returned. when n=4 When, printed 1234n=8 At the time, all elements were printed. feel like mysqThe pagination of the page has the same effect.

4. difference

Both methods intercept the stream. But there are some differences skip Operations must always monitor the status of elements in the flow. To determine whether it needs to be discarded. so skip It belongs to state operation.

and limit We only care about whether we intercept its parameters maxsize (Maximum interval value), nothing else cares. Once reached, the operation is interrupted immediately and returned to the flow. so limit It belongs to an interrupt operation.

5. summary

Today's review of the Java Stream API skip() andlimit()Methods were discussed. I don't know what scenarios you will think of using them separately, so you might as well leave a message and let me know.

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/4441.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

美团代付 支持多模板全开源 多种支付通道 多模版三合一源码 附教程
Meituan payment supports multiple templates, full open source, multiple payment channels, multiple templates, three-in-one source code with tutorials
Someone bought it 10 minutes ago 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