java集合分割成等份的小集合:

 

 private <T> List<List<T>> getSubList(List list,int len) {
        if(list.size()<len){
            return list;
        }
        List<List<T>> listGroup = new ArrayList<List<T>>();
        int listSize = list.size();
        //子集合的长度
        int toIndex = len;
        for (int i = 0; i < list.size(); i += len) {
            if (i + len > listSize) {
                toIndex = listSize - i;
            }
            List<T> newList = list.subList(i, i + toIndex);
            listGroup.add(newList);
        }
        return listGroup;
    }

 

eg:

    /**
     * @return
     * @Author
     * @Description //TODO 集合分割
     * @Date 2019/1/24 16:48
     * @Param
     */
    private List<List<PushResult>> getSubList(List list, int len) {
        List<List<PushResult>> listGroup = new ArrayList<List<PushResult>>();
        if (list.size() < len) {
            listGroup.add(list);
            return listGroup;
        }

        int listSize = list.size();
        //子集合的长度
        int toIndex = len;
        for (int i = 0; i < list.size(); i += len) {
            if (i + len > listSize) {
                toIndex = listSize - i;
            }
            List<PushResult> newList = list.subList(i, i + toIndex);
            listGroup.add(newList);
        }
        return listGroup;
    }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
  • 2021-11-06
  • 2022-01-10
  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
猜你喜欢
  • 2021-07-02
  • 2021-05-30
  • 2022-12-23
  • 2022-12-23
  • 2022-03-10
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案