赞
踩
- package com.demo.domain.utils;
-
- import com.alibaba.fastjson.JSONObject;
- import com.google.common.collect.Lists;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
-
- /**
- * @Description:
- * @title: ListUtils
- * @Version 1.0
- */
- public class ListUtils {
-
- /**
- * @Description 对list进行分页
- * @Date 2022/3/10 11:01
- * @Param [nlist, pageNum]
- * @return
- **/
- public static <T> List<List<T>> getPageData(List<T> nlist, int pageNum) {
- if (nlist == null || nlist.size() == 0) {
- return null;
- }
- List<List<T>> result = new ArrayList<>();
- if (pageNum < 1) {
- result.add(nlist);
- return result;
- }
-
- for (int i = 0; i < Math.ceil((double)nlist.size() / pageNum); i++) {
- List<T> tempList = nlist.stream().skip(i * pageNum).limit(pageNum).collect(Collectors.toList());
- result.add(tempList);
- }
- return result;
- }
-
- public static void main(String[] args) {
- List<String> list = Lists.newArrayList("1","2","3","4","5","6","7","8","9");
- List<List<String>> pageData = getPageData(list, 3);
- for (List<String> data : pageData) {
- System.out.println(JSONObject.toJSONString(data));
- }
- }
-
- }
运行main方法:
["1","2","3"]
["4","5","6"]
["7","8","9"]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。