简体   繁体   English

Java 8流,List.subList或Stream.skip()。limit()

[英]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 . 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. 尽管skip()在顺序流管道上通常是便宜的操作,但在有序并行管道上可能会非常昂贵,尤其是对于较大的n值,因为skip(n)不仅被限制为不仅跳过任何n个元素,而且还跳过第一个n遇到顺序中的元素。 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. 如果情况的语义允许,则使用无序流源(例如generate(Supplier))或使用BaseStream.unordered()删除排序约束可能会导致并行管道中skip()的显着加速。 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. 如果需要与遇到顺序保持一致,并且在并行管道中使用skip()遇到性能低下或内存使用不足的情况,则切换为使用BaseStream.sequential()顺序执行可能会提高性能。

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 . 流可以代替循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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