BeanUtils.java 813 B

12345678910111213141516171819202122232425262728
  1. package com.poyee.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. /**
  6. * @param <T>
  7. */
  8. @Slf4j
  9. public class BeanUtils<T> {
  10. public static<T> List<List<T>> batchList(List<T> sourceList, int batchCount) {
  11. List<List<T>> returnList = new ArrayList<>();
  12. int startIndex = 0; // 从第0个下标开始
  13. while (startIndex < sourceList.size()) {
  14. int endIndex = 0;
  15. if (sourceList.size() - batchCount < startIndex) {
  16. endIndex = sourceList.size();
  17. } else {
  18. endIndex = startIndex + batchCount;
  19. }
  20. returnList.add(sourceList.subList(startIndex, endIndex));
  21. startIndex = startIndex + batchCount; // 下一批
  22. }
  23. return returnList;
  24. }
  25. }