1. Preface
In Java development, some scenarios are relatively complex and restricted. It is not easy to implement paging at the SQL query level. You need to return the list paging after the list result of the query. How to implement it?
2. Code implementation
1. Suppose there is a list collection with the elements as follows:
ArrayList<Integer> list = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
2. Use subList to implement pagination:
/** *@Description: subList pagination * <br> 1. Start position boundary value processing: Current page number <= 0 situation processing; Current page number > Maximum page number Situation processing * <br> 2. Termination position boundary value processing: Current page number <= 0 situation processing; Termination position <= Total number of records situation processing * @param pageSize Number of displayed per page * @param pageNum Current page number */ private List<?> subList(ArrayList<?> list, int pageSize, int pageNum) { int count = (); // Total records // Calculate the total number of pages int pages = count % pageSize == 0 ? count / pageSize : count / pageSize + 1; // Start position int start = pageNum <= 0 ? 0 : (pageNum > pages ? (pages - 1) * pageSize : (pageNum - 1) * pageSize); // Termination position int end = pageNum <= 0 ? (pageSize <= count ? pageSize : count) : (pageSize * pageNum <= count ? pageSize * pageNum : count); return (start, end); }
3. Java8 Stream Pagination
/**@Description: Java8 Stream Pagination * <br> 1. Start position boundary value processing: same as subList method * <br> 2. Termination location: No processing is required, boundary issues will be automatically handled * @param pageSize Number of displayed per page * @param pageNum Current page number */ private List<?> subListJava8(ArrayList<?> list, int pageSize, int pageNum) { int count = (); // Total records // Calculate the total number of pages int pages = count % pageSize == 0 ? count / pageSize : count / pageSize + 1; // Start position int start = pageNum <= 0 ? 0 : (pageNum > pages ? (pages - 1) * pageSize : (pageNum - 1) * pageSize); // Termination position int end = pageSize ; return ().skip(start).limit(pageSize).collect(()); }
4. Use the partition pagination of the Lists tool class
/**@Description: Partition pagination using Lists tool class * <br> 1. Page number boundary value processing: less than or equal to 0, case processing; Page number is greater than (total number of pages-1) case processing * <br> 2. Total number of pages -1, the reason is: the list collection index starts from 0. * @param pageSize Number of displayed per page * @param pageNum Current page number * @see #partition(, int) */ private List<?> partition(ArrayList<?> list, int pageSize, int pageNum) { // Split List into multiple Lists according to PageSzie List<? extends List<?>> partition = (list, pageSize); // Total page count int pages = (); pageNum = pageNum <= 0 ? 0 : (pageNum <= (pages - 1) ? pageNum : (pages - 1)); return (pageNum); }
3. Summary
No matter which way of implementing paging is above, it will have a certain impact on performance, especially the larger the data volume, the worse the performance, because the principle is to scan the full table, query all the data and then return it in memory. If possible, try to think about how to implement paging return during SQL query.
This is the article about Java collection implementation paging - business code implementation paging. For more related content on Java collection implementation paging, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!