Why do you need to split a large array into equal-sized chunks, and do you
really want them all allocated at once?

Most (JDK, at least) APIs that take arrays have more flexible versions that
take an array, a start offset, and a length. With these there's no need to
copy the array into smaller chunks. You just need the offset/length tuples.

And if you do need smaller chunks, you likely only need them one-by-one.
Allocating them all at once doubles your memory footprint.

Peter

On Fri, Sep 5, 2025, 9:39 PM Kushal Dixit <[email protected]> wrote:

> ArrayUtils.partition will split arrays into fixed-size chunks.
> ListUtils.partition can’t be used directly on arrays, and converting arrays
> to lists adds overheads, especially for primitives. This utility will work
> directly on arrays and simplify common tasks.
>
> For example, I once had to split a large byte[] into equal-sized chunks and
> realized I wished a ready-made method existed.
>
> ArrayUtils.partition will be definitely useful for byte arrays for now. I’m
> not sure about other array types yet.
>
> Kushal
>
> On Sat, 6 Sept, 2025, 2:44 am sebb, <[email protected]> wrote:
>
> > On Fri, 5 Sept 2025 at 18:46, Kushal Dixit <[email protected]> wrote:
> > >
> > > Hi All,
> > >
> > > I’d like to propose adding a small utility to ArrayUtils for
> partitioning
> > > arrays into fixed-size chunks.
> > >
> > > Currently, ListUtils.partition exists for lists, but there is no direct
> > > equivalent for arrays in Commons Lang. Developers often write custom
> > loops
> > > to achieve the same result.
> > >
> > > Proposed API example:
> > >
> > > int[][] result = ArrayUtils.partition(new int[]{1, 2, 3, 4, 5}, 2);
> > > // → [[1, 2], [3, 4], [5]]
> > >
> > > Key points:
> > >
> > > Supports primitive and object arrays.
> > >
> > > Null-safe (returns empty array of arrays if input is null).
> > >
> > > Consistent with ListUtils.partition behavior.
> > >
> > > Small, general-purpose utility aligned with Lang’s focus on array
> > helpers.
> > >
> > >
> > > I’d like to check whether the community thinks this addition fits
> Lang’s
> > > scope before opening a JIRA/PR.
> >
> > What use cases are there for this?
> >
> > > Thanks,
> > > Kushal
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [email protected]
> > For additional commands, e-mail: [email protected]
> >
> >
>

Reply via email to