[Java] FIG compulsory Stream in said Skip () and the limit () method and a combination

1 Introduction

This article will explain the two methods in Java 8 Stream: skip()and limit(). These two methods are Streamvery common, not only is each used frequency, can also occur in combination, and can achieve small features, such as subListand the like tab.

2 skip () method

See name known sense, skip()a method for skip ahead n elements, then returns the new stream, as shown:

Look at the code:

List<Integer> result = Stream.of(1, 2, 3, 4, 5, 6)
  .skip(4)
  .collect(Collectors.toList());
List<Integer> expected = asList(5, 6);
assertEquals(expected, result);

The method of skip()parameter n four cases:

(1) When n<0time, throw IllegalArgumentExceptionexception;

(2) When n=0time, without skipping any element rather, intact, retrieve their full property;

(3) When 0<n<lengththe time, skip n elements, comprising the rest of the return flow element;

(4) When n>=lengthtime, skip all the elements, the flow returns empty.

3 limit () method

For the limit()method, which is the number of elements for limiting the flow, i.e., taking the first n elements, returns the new stream, as shown:

code show as below:

List<Integer> result = Stream.of(1, 2, 3, 4, 5, 6)
  .limit(4)
  .collect(Collectors.toList());
List<Integer> expected = asList(1, 2, 3, 4);
assertEquals(expected, result);

The method of limit()parameter n four cases:

(1) When n<0time, throw IllegalArgumentExceptionexception;

(2) when n=0, there are no elements having, flow returns empty;

(3) when 0<n<length, the first n elements taken, and returns the new flow;

(4) When n>=lengthtime, taking all the elements, intact, retrieve their full property.

Four pairs of operation unlimited stream

Stream flow into finite and infinite stream flow restrictor previous example we are using, and the Java collection classes, streaming can be unlimited. For an infinite stream, skip()and limit()showed great differences, the first on the code:

Stream.iterate(1, i -> i + 1)
  .filter(num -> (num & (num - 1)) == 0)
  .limit(10)
  .forEach(System.out::println);
System.out.println("----------------");
Stream.iterate(1, i -> i + 1)
  .filter(num -> (num & (num - 1)) == 0)
  .skip(10)
  .forEach(System.out::println);

执行后发现,limit()是可以将无限流转化为有限流的,所以我们也可以认为它是一个短路操作。而skip()则不行,不管你跳过了前面多少个元素,总还是会有源源不断的元素过来,无法收敛。

上述代码的结果是:

通过limit()输出了前十个2的n次方值:
1, 2, 4, 8, 16, 32, 64, 128, 256, 512

skip()跳过了前10个,继续输出,但会不断执行下去(会有int的溢出现象):

1024, 2048, 4096, 8192, 16384, 32768...

5 组合应用

除了两者各自有各自的功能外,我们通过组合使用,可以实现其它功能。

5.1 与subList的替换

集合类如List是有subList()这个方法的,可以截取List中的某一部分,这个功能还可以通过组合skip()limit()使用得到,如下面代码:

List<Integer> list = asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
List<Integer> expected = list.subList(3, 7);

List<Integer> result = list.stream()
  .skip(3)
  .limit(7 - 3)
  .collect(Collectors.toList());
assertEquals(expected, result);

subList(startIndex, endIndex)转换成skip(startIndex).limit(endIndex - startIndex)

5.2 分页

可以通过组合使用skip()limit()进行分页,如下面代码:

int pageSize = 10;
int pageIndex = 7;

List<Integer> expected = asList(61, 62, 63, 64, 65, 66, 67, 68, 69, 70);
List<Integer> result = Stream.iterate(1, i -> i + 1)
  .skip((pageIndex - 1) * pageSize)
  .limit(pageSize)
  .collect(Collectors.toList());

assertEquals(expected, result);

上面代码例子是获取了第七页数据,每页大小为10。

6 总结

本文介绍了Java 8的Stream接口中两个常用的方法:skip()limit(),比较简单易懂,也介绍了怎么组合使用。需要注意的是,如果Stream过大或是无限流,小心skip()会有性能问题。


欢迎关注公众号<南瓜慢说>,将持续为你更新...

欢迎加博主微信,做一个点赞之友,哈哈...

多读书,多分享;多写作,多整理。

Guess you like

Origin www.cnblogs.com/larrydpk/p/12078124.html