[PATCH] Duplicated indexOf/lasIndexOf in array-based collections
paul.sandoz at oracle.com
Mon Mar 26 20:39:02 UTC 2018
> On Mar 26, 2018, at 11:46 AM, Martin Buchholz <martinrb at google.com> wrote:
> Сергей, I'm sort of maintaining all those collection classes, and I have on
> occasion also wanted to have indexOf methods in Array.java. But:
> Arrays are generally discouraged. Any new static methods on Arrays (or,
> where I actually want them, on the array object itself! Requires a java
> language change!) will meet resistance.
I would not go so far as to say arrays are discouraged, i would positively spin it as "use with care” as they are prickly e.g. always mutable. They could certainly be improved. I would be very happy to see arrays implement a common array’ish interface, we might be able to make some progress after value types sediment.
Any new additions to Arrays would be met with some resistance though, by me at least :-) It’s never adding just one or two methods, many others want come along for the ride as well (all primitives plus range variants). So any new feature needs to be sufficiently beneficial and in this case i don’t think the benefits are strong enough (such as a possible reduction code cache pressure).
> We've come to regret supporting nulls in collections, so newer collection
> classes like ArrayDeque don't support them.
> Another variant users might want is what kind of equality comparison to use.
> We've come to regret having ArrayList with zero-based start index - it
> would have been better to have ArrayDeque's circular array behavior in from
> day one.
> The code to search array slices is very small, so you're not saving much.
> It's easy to make an off-by-one error, but that's true for an Arrays API as
> On Sat, Mar 10, 2018 at 12:42 AM, Сергей Цыпанов <sergei.tsypanov at yandex.ru>
>> Hi everyone,
>> currently array-based collections, in particular java.util.ArrayList,
>> java.util.Arrays$ArrayList, java.util.Vector, java.util.concurrent.CopyOnWriteArrayList
>> have duplicated code in indexOf() and lastIndexOf().
>> My proposal is to extract this code into java.util.Arrays helping JIT and
>> saving space in ReservedCodeCache.
>> Moreover, developers quite often use code snippets like
>> Arras.asList(array).contains(obj) or even
>> Arrays.stream(names).anyMatch(existing -> existing.equals(name));
>> or write similar utility code for allocation-free cases like the one in
>> Also see
>> If java.util.Arrays incorporates contains() and indexOf() then all that
>> boilerplate can be replaced with usage of JDK-provided standard API.
>> Patch is attached to this mail.
>> Sergey Tsypanov
More information about the core-libs-dev