Java中的 stream.limit() 方法
先决条件: Java8 中的流
limit(long N)是Java.util.stream.Stream对象的一种方法。此方法将一个(长 N)作为参数并返回一个大小不超过 N 的流。limit() 在有序并行管道上可能非常昂贵,如果 N 的值很大,因为 limit(N) 被限制为返回遇到顺序中的前 N 个元素,而不仅仅是任何 n 个元素。
句法 :
Stream limit(long N)
Where N is the number of elements the stream should be
limited to and this function returns new stream as output.
异常:如果 N 的值为负,则函数抛出IllegalArgumentException 。
下面是一些示例,可以更好地理解函数的实现。
示例 1:
// Java code to show implementation
// of limit() function
import java.util.*;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating a list of integers
List list = new ArrayList();
// adding elements in the list
list.add(-2);
list.add(0);
list.add(2);
list.add(4);
list.add(6);
list.add(8);
list.add(10);
list.add(12);
list.add(14);
list.add(16);
// setting the value of N as 4
int limit = 4;
int count = 0;
Iterator it = list.iterator();
// Iterating through the list of integers
while (it.hasNext()) {
it.next();
count++;
// Check if first four i.e, (equal to N)
// integers are iterated.
if (count > limit) {
// If yes then remove all the remaining integers.
it.remove();
}
}
System.out.print("New stream of length N"
+ " after truncation is : ");
// Displaying new stream of length
// N after truncation
for (Integer number : list) {
System.out.print(number + " ");
}
}
}
输出 :
New stream of length N after truncation is : -2 0 2 4
应用 :
// Java code to show the use of limit() function
import java.util.stream.Stream;
import java.util.ArrayList;
import java.util.List;
class gfg{
// Function to limit the stream upto given range, i.e, 3
public static Stream limiting_func(Stream ss, int range){
return ss.limit(range);
}
// Driver code
public static void main(String[] args){
// list to save stream of strings
List arr = new ArrayList<>();
arr.add("geeks");
arr.add("for");
arr.add("geeks");
arr.add("computer");
arr.add("science");
Stream str = arr.stream();
// calling function to limit the stream to range 3
Stream lm = limiting_func(str,3);
lm.forEach(System.out::println);
}
}
输出 :
geeks
for
geeks