Collections.emptyList().spliterator() is not ORDERED
Paul Sandoz
paul.sandoz at oracle.com
Mon Sep 14 10:24:35 UTC 2015
On 7 Sep 2015, at 15:23, Paul Sandoz <Paul.Sandoz at oracle.com> wrote:
>> By the way, probably it's reasonable then for Arrays.asList to check
>> the array length like:
>>
>> public static <T> List<T> asList(T... a) {
>> if(a.length == 0)
>> return Collections.emptyList();
>> return new ArrayList<>(a);
>> }
>>
>> This would make Arrays.asList() (without arguments) and
>> Collections.emptyList() perfectly consistent (now their spliterators
>> report different characteristics) and reduce the number of heap
>> objects. Probably there are some caveats I'm not aware of. Sorry if it
>> was already discussed.
>>
>
> This has not been discussed, it’s an edge case micro-optimisation but seems reasonable.
>
There is a reason not to do this. At the moment Arrays.asList specifies no constraints on the identity of the returned List. Adding the micro-optimisation will change that. It’s an edge case and a questionable use-case too, but considering that i would conservatively leave things as they are.
Paul.
More information about the core-libs-dev
mailing list