简体   繁体   中英

Java 8 stream, List.subList or Stream.skip().limit()

I need to do a treatment on a page of a list. So I'm wondering what is better (in performance and in good practice) between :

public List<Other> function(List<Something> somethingList, 
                            int offset, int pageSize) {
    return somethingList.stream().skip(offset * pageSize).limit(pageSize)
        .map(s -> doSomething(s)).collect(Collectors.toList());
}

or :

public List<Other> function(List<Something> somethingList, 
                            int offset, int pageSize) {
    return somethingList.subList(offset * pageSize, offset * pageSize + pageSize)
        .stream().map(s -> doSomething).collect(Collectors.toList());
}

I have omitted voluntary the validation of offset and page size.

Edit: It is not a vital need of performance, my question is also about clarity, maintenance and good practice. I prefer the stream's way especially because it needs less verification. But I prefer to be sure that is not a real lost of performance or a less maintainable/clean things.

The difference is the way the stream operations are ordered. Java Stream API . States that skip is cheap.

While skip() is generally a cheap operation on sequential stream pipelines, it can be quite expensive on ordered parallel pipelines, especially for large values of n, since skip(n) is constrained to skip not just any n elements, but the first n elements in the encounter order. Using an unordered stream source (such as generate(Supplier)) or removing the ordering constraint with BaseStream.unordered() may result in significant speedups of skip() in parallel pipelines, if the semantics of your situation permit. If consistency with encounter order is required, and you are experiencing poor performance or memory utilization with skip() in parallel pipelines, switching to sequential execution with BaseStream.sequential() may improve performance.

and

Limit: Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.

One benefit to using streams over sub-list is that you can apply filters and or the logic you ask about and it will likely be cheaper then making a sub-list. While the stream happens in order of the functions, some elements may be filtered out and you only have to do the stream once. While list items you may have to loop multiple times and use multiple objects to temporary hold the items; often times looping over the same unneeded function for that item.

While your question is very specific. These same principles would apply to whats happening under the hood in the list. Power of streams . Under the hood you still may have multiple objects from a stream; but the complexity is taken away from the programmer when doing complex operations on a collection of elements. Simply put it can replace many back to back for loops where we use top process elements. They really are utility. Streams can replace for loops .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM