import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SubListTest
{
public static void main(String[] args)
{
List<String> alphabetNames = new ArrayList<String>();
// populate alphabetNames array with AAA,BBB,CCC,.....
int a = (int) 'A';
for (int i = 0; i < 26; i++)
{
char x = (char) (a + i);
char[] array = new char[3];
Arrays.fill(array, x);
alphabetNames.add(new String(array));
}
int[] maxListSizes = new int[]
{
5, 10, 15, 20, 25, 30
};
for (int maxListSize : maxListSizes)
{
System.out.println("######################################################");
System.out.println("Partitioning original list of size " + alphabetNames.size() + " in to sub lists of max size "
+ maxListSize);
ArrayList<List<String>> subListArray = new ArrayList<List<String>>();
if (alphabetNames.size() <= maxListSize)
{
subListArray.add(alphabetNames);
}
else
{
// based on subLists of maxListSize X
int subListArraySize = (alphabetNames.size() + maxListSize - 1) / maxListSize;
for (int i = 0; i < subListArraySize; i++)
{
subListArray.add(alphabetNames.subList(i * maxListSize,
Math.min((i * maxListSize) + maxListSize, alphabetNames.size())));
}
}
System.out.println("Resulting number of partitions " + subListArray.size());
for (List<String> subList : subListArray)
{
System.out.println(subList);
}
}
}
}
Output:
######################################################
Partitioning original list of size 26 in to sub lists of max size 5
Resulting number of partitions 6
[AAA, BBB, CCC, DDD, EEE]
[FFF, GGG, HHH, III, JJJ]
[KKK, LLL, MMM, NNN, OOO]
[PPP, QQQ, RRR, SSS, TTT]
[UUU, VVV, WWW, XXX, YYY]
[ZZZ]
######################################################
Partitioning original list of size 26 in to sub lists of max size 10
Resulting number of partitions 3
[AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH, III, JJJ]
[KKK, LLL, MMM, NNN, OOO, PPP, QQQ, RRR, SSS, TTT]
[UUU, VVV, WWW, XXX, YYY, ZZZ]
######################################################
Partitioning original list of size 26 in to sub lists of max size 15
Resulting number of partitions 2
[AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH, III, JJJ, KKK, LLL, MMM, NNN, OOO]
[PPP, QQQ, RRR, SSS, TTT, UUU, VVV, WWW, XXX, YYY, ZZZ]
######################################################
Partitioning original list of size 26 in to sub lists of max size 20
Resulting number of partitions 2
[AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH, III, JJJ, KKK, LLL, MMM, NNN, OOO, PPP, QQQ, RRR, SSS, TTT]
[UUU, VVV, WWW, XXX, YYY, ZZZ]
######################################################
Partitioning original list of size 26 in to sub lists of max size 25
Resulting number of partitions 2
[AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH, III, JJJ, KKK, LLL, MMM, NNN, OOO, PPP, QQQ, RRR, SSS, TTT, UUU, VVV, WWW, XXX, YYY]
[ZZZ]
######################################################
Partitioning original list of size 26 in to sub lists of max size 30
Resulting number of partitions 1
[AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH, III, JJJ, KKK, LLL, MMM, NNN, OOO, PPP, QQQ, RRR, SSS, TTT, UUU, VVV, WWW, XXX, YYY, ZZZ]