List集合切分若干份

  public static List> splitList(List list, int n) {
         List> strinList = new ArrayList<>();
         if (list == null) return strinList;

         int size = list.size();

         int quotient = size / n; // 商数
         int remainder = size % n; // 余数
         int offset = 0; // 偏移
         int len = quotient > 0 ? n : remainder; // 循环长度
         int start = 0;    // 开始下标
         int end = 0;    // 结束下标
         List list2 = null;
         for (int i = 0; i < len; i++) {
             if (remainder != 0) {
                 remainder--;
                 offset = 1;
             } else {
                 offset = 0;
             }
             end = start + quotient + offset;
            list2 = list.subList(start, end);
             start = end;
             strinList.add(list2);
         }
         return strinList;
     }

你可能感兴趣的:(List集合切分若干份)