How to partition a list with N partitions rather than partitions of size N?

Jacob G. :

Guava's method, Lists#partition, partitions a List<?> into a List<List<?>> where each partition contains N elements (as specified by the second parameters of the function, and excluding the last partition).

Is it possible to use this method but create N partitions instead?

If not, what are some ways to go about it?

I've attempted to create 31 partitions with the following (keys is a List<String> of size 57), but it only creates 29:

List<String> keys = ...;

var paritions = Lists.partition(keys, (int) Math.ceil(keys.size() / 31D));
Makoto :

To be able to create N partitions, you have to have a minimum of 2N elements. In your case, you have a partition requirement of 31, which means you'd need 62 elements.

Because you have 57 elements, you're five elements - or two and a half partitions - short of the required minimum, which is why you get 29 partitions, with the last partition only having one element.

Guava is doing its job. You don't have enough elements to properly subdivide into the partitions you want.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=111829&siteId=1