Search code examples
javastringlistguavaapache-commons

how to split a list into a given number of sub-lists?


I have a list that may be over 1000 strings, however I do not know how many exactly.

What is the best way to split this list into smaller lists without loosing any members of the list?

For example If I have a list of 1323 members, how can I best split it into 3 almost evenly sized lists?

I have seen the Guava and Commons way of splitting lists by the partition function, but that function will split the list into given size of chunks and not given number of groups (sub-lists).


Solution

  • Guava has a function Lists.partition which will do this for you

    Usage:

    Lists.partition(mylist, mylist.size()/3);