1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| import com.google.common.collect.Lists; import org.apache.commons.collections.CollectionUtils;
import java.util.List;
public class SplitListUtils {
public static <T> List<List<T>> split(List<T> resList, int subListLength) { if (CollectionUtils.isEmpty(resList) || subListLength <= 0) { return Lists.newArrayList(); } List<List<T>> ret = Lists.newArrayList(); int size = resList.size(); if (size <= subListLength) { ret.add(resList); } else { int pre = size / subListLength; int last = size % subListLength; for (int i = 0; i < pre; i++) { List<T> itemList = Lists.newArrayList(); for (int j = 0; j < subListLength; j++) { itemList.add(resList.get(i * subListLength + j)); } ret.add(itemList); } if (last > 0) { List<T> itemList = Lists.newArrayList(); for (int i = 0; i < last; i++) { itemList.add(resList.get(pre * subListLength + i)); } ret.add(itemList); } } return ret; }
public static void main(String[] args) { List<String> list = Lists.newArrayList(); int size = 1099; for (int i = 0; i < size; i++) { list.add("hello-" + i); } List<List<String>> temps = split(list, 100); int j = 0; for (List<String> obj : temps) { System.out.println(String.format("row:%s -> size:%s,data:%s", ++j, obj.size(), obj)); } }
}
|