import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
public class Demo {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
AtomicInteger my_index = new AtomicInteger();
int[] arr2 = Arrays.stream(arr).filter(item -> {
int index = my_index.getAndIncrement();
return (index >= 2 && index < 7) ? true : false;
}).toArray();
}
}
知识点:
1、流中使用索引的方法,使用的是一个自增序列,假装它是这个流的索引;
2、通过filter自定义过滤逻辑替代skip和limit。