Collections.emptyList().sort() does nothing

Andrej Golovnin andrej.golovnin at gmail.com
Wed Nov 15 10:21:06 UTC 2017


> On 15/11/17 10:03, Andrej Golovnin wrote:
>> I think we would need to write ugly code in any case as Java 9 has now
>> two empty list implementations: Collections.emptyList() and List.of().
>>
>> Collections.emptyList().sort() does not throw an exception.
>>
>> List.of().sort() throws an exception.
>
> Well, that's a bug.  Sorting an empty list is perfectly valid, and application
> programmers shouldn't have to special-case it.

I don't think so. I would say that the implementation of
Collections.emptyList() is broken. Collections.emptyList() and
List.of() are not only empty lists. They are also immutable per
specification.

Here is another example of an unsortable empty list:

Collections.unmodifiableList(Collections.emptyList()).sort() throws an
exception.

>
> --
> Andrew Haley
> Java Platform Lead Engineer
> Red Hat UK Ltd. <https://www.redhat.com>
> EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671


More information about the core-libs-dev mailing list