Here you can find the source of partition(List
Parameter | Description |
---|---|
longList | a parameter |
length | a parameter |
public static <T> List<List<T>> partition(List<T> longList, int length)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.List; public class Main { /**// w w w. j av a 2 s . c o m * * @param longList * @param length * @return */ public static <T> List<List<T>> partition(List<T> longList, int length) { List<List<T>> result = new ArrayList<List<T>>(); if (longList == null || longList.isEmpty()) return result; if (longList.size() <= length) { result.add(longList); } else { int groups = (longList.size() - 1) / length + 1; for (int i = 0; i < groups - 1; i++) { result.add(new ArrayList(longList.subList(length * i, length * (i + 1)))); } result.add(new ArrayList(longList.subList(length * (groups - 1), longList.size()))); } return result; } }