The Stream API was introduced in Java 8, which can greatly simplify the operation of collections. The Stream class provides many functional methods for operating on streams, including filtering, mapping, merging, and more. Among them, limit and skip are two functions used to limit the number of elements in stream operations.
1. Limit function
The limit function is used to limit the number of elements in the stream. It accepts a long type parameter n, indicating the number of limits. After calling the limit function, a new stream is returned, which only contains the first n elements in the original stream. For example:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); list.stream().limit(3).forEach(System.out::print);
The output result of the above code is: 1 2 3. In this example, we convert a List collection into a Stream through the stream method, then use the limit method to limit the elements in the stream, and finally output the result through the forEach method.
2. Skip function
The skip function is used to skip elements in the stream. It accepts a long type parameter n, indicating the number to be skipped. After calling the skip function, a new stream is returned, which skips the first n elements in the original stream. For example:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); list.stream().skip(2).forEach(System.out::print);
The output result of the above code is: 3 4 5. In this example, we convert a List collection into a Stream through the stream method, then use the skip method to skip the first two elements, and finally output the result through the forEach method.
3. Use the limit and skip functions to implement paging
Using the limit and skip functions together can easily implement the paging function. Suppose we have a list containing N elements, and we want to implement a paging function to display M elements on each page. We can implement it in the following way:
public class PaginationDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); for (int i = 1; i <= 10; i++) { list.add(i); } int pageSize = 3; int pageNum = 1; int start = (pageNum - 1) * pageSize; int end = pageNum * pageSize; List<Integer> result = list.stream().skip(start).limit(end - start).collect(Collectors.toList()); System.out.println(result); } }
In the above code, we first create a list containing 10 elements. Then, we define the number pageSize and page number pageNum displayed on each page, and calculate the number of elements that need to be skipped start and the number of elements that need to be filtered end-start. Finally, we use the limit and skip functions to filter out the elements required for the specified page number from the list, and collect the results into the list through the collect method.
The above is the application of limit and skip functions. They can help us easily limit and skip elements in the stream and realize the paging function of stream operations.
The above is the detailed content of How to use the limit and skip functions of Stream in Java for stream operations. For more information, please follow other related articles on the PHP Chinese website!