package com.founder.tongyin.util;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @ClassName: ListUtil
 * @author: dh
 * @since: 2020/8/3 15:44
 */
public class ListUtil {

    public static <T> List<List<T>> splitList(List<T> list, int groupSize){
        int length = list.size();
        int num = ( length + groupSize - 1 )/groupSize ;
        List<List<T>> newList = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            int fromIndex = i * groupSize;
            int toIndex = (i+1) * groupSize < length ? ( i+1 ) * groupSize : length ;
            newList.add(list.subList(fromIndex,toIndex)) ;
        }
        return  newList ;
    }


    public static <T> List<List<T>> splitListToNum(List<T> list, int num){
        int length = list.size();
        int groupSize = (int)Math.ceil(length*1.0 / num);
        List<List<T>> newList = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            int fromIndex = i * groupSize;
            int toIndex = (i+1) * groupSize < length ? ( i+1 ) * groupSize : length ;
            newList.add(list.subList(fromIndex,toIndex)) ;
        }
        return  newList ;
    }

}

 

相关文章:

  • 2021-08-04
  • 2022-12-23
  • 2021-09-16
  • 2021-07-26
  • 2022-12-23
  • 2021-07-09
  • 2021-09-21
  • 2022-02-07
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案