Java List Split splitList(List sourceList, int spliTotal)

Here you can find the source of splitList(List sourceList, int spliTotal)

Description

split List

License

Apache License

Declaration

public static <T> List<List<T>> splitList(List<T> sourceList, int spliTotal) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {

    public static <T> List<List<T>> splitList(List<T> sourceList, int spliTotal) {

        List<List<T>> resultList = new ArrayList<List<T>>();

        int pagesize = spliTotal;

        int totalcount = sourceList.size();

        int pagecount = 0;

        int m = totalcount % pagesize;

        if (m > 0) {
            pagecount = totalcount / pagesize + 1;
        } else {//from   ww  w  . ja va2s  . c om
            pagecount = totalcount / pagesize;
        }

        for (int i = 1; i <= pagecount; i++) {
            if (m == 0) {
                List<T> subList = sourceList.subList((i - 1) * pagesize, pagesize * (i));
                resultList.add(subList);
            } else {
                if (i == pagecount) {
                    List<T> subList = sourceList.subList((i - 1) * pagesize, totalcount);
                    resultList.add(subList);
                } else {
                    List<T> subList = sourceList.subList((i - 1) * pagesize, pagesize * (i));
                    resultList.add(subList);
                }
            }
        }

        return resultList;
    }
}

Related

  1. splitList(List list, int number)
  2. splitList(List _list, int _elements)
  3. splitList(List list, int page)
  4. splitList(List parent, int subSize)
  5. splitList(List source, int count)
  6. splitListIntoParts(List list, int sizeOfEachPart)