You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Kushal Dixit <kd...@gmail.com> on 2025/09/05 17:45:45 UTC

[PROPOSAL] ArrayUtils.partition helper method.

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.

Thanks,
Kushal

Re: [PROPOSAL] ArrayUtils.partition helper method.

Posted by Kushal Dixit <kd...@gmail.com>.
Thanks for the feedback. I agree almost all JDK APIs support offset and
length, so this utility isn’t strictly needed. The library I was working
with at the time may not have been designed to handle that efficiently.

I’ll revisit if a strong use case arises in the future. For now, I’m
withdrawing the proposal.

Kushal

On Sat, 6 Sept, 2025, 7:40 am Peter Burka, <pe...@quux.net> wrote:

> 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 <kd...@gmail.com> 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, <se...@gmail.com> wrote:
> >
> > > On Fri, 5 Sept 2025 at 18:46, Kushal Dixit <kd...@gmail.com>
> 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: dev-unsubscribe@commons.apache.org
> > > For additional commands, e-mail: dev-help@commons.apache.org
> > >
> > >
> >
>

Re: [PROPOSAL] ArrayUtils.partition helper method.

Posted by Peter Burka <pe...@quux.net>.
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 <kd...@gmail.com> 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, <se...@gmail.com> wrote:
>
> > On Fri, 5 Sept 2025 at 18:46, Kushal Dixit <kd...@gmail.com> 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: dev-unsubscribe@commons.apache.org
> > For additional commands, e-mail: dev-help@commons.apache.org
> >
> >
>

Re: [PROPOSAL] ArrayUtils.partition helper method.

Posted by Kushal Dixit <kd...@gmail.com>.
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, <se...@gmail.com> wrote:

> On Fri, 5 Sept 2025 at 18:46, Kushal Dixit <kd...@gmail.com> 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: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Re: [PROPOSAL] ArrayUtils.partition helper method.

Posted by sebb <se...@gmail.com>.
On Fri, 5 Sept 2025 at 18:46, Kushal Dixit <kd...@gmail.com> 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: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org