当前位置:   article > 正文

对list进行分页_domain 对象中list属性如何分页显示

domain 对象中list属性如何分页显示
  1. package com.demo.domain.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.google.common.collect.Lists;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.stream.Collectors;
  7. /**
  8. * @Description:
  9. * @title: ListUtils
  10. * @Version 1.0
  11. */
  12. public class ListUtils {
  13. /**
  14. * @Description 对list进行分页
  15. * @Date 2022/3/10 11:01
  16. * @Param [nlist, pageNum]
  17. * @return
  18. **/
  19. public static <T> List<List<T>> getPageData(List<T> nlist, int pageNum) {
  20. if (nlist == null || nlist.size() == 0) {
  21. return null;
  22. }
  23. List<List<T>> result = new ArrayList<>();
  24. if (pageNum < 1) {
  25. result.add(nlist);
  26. return result;
  27. }
  28. for (int i = 0; i < Math.ceil((double)nlist.size() / pageNum); i++) {
  29. List<T> tempList = nlist.stream().skip(i * pageNum).limit(pageNum).collect(Collectors.toList());
  30. result.add(tempList);
  31. }
  32. return result;
  33. }
  34. public static void main(String[] args) {
  35. List<String> list = Lists.newArrayList("1","2","3","4","5","6","7","8","9");
  36. List<List<String>> pageData = getPageData(list, 3);
  37. for (List<String> data : pageData) {
  38. System.out.println(JSONObject.toJSONString(data));
  39. }
  40. }
  41. }

运行main方法:

["1","2","3"]
["4","5","6"]
["7","8","9"]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/1022292
推荐阅读
相关标签
  

闽ICP备14008679号