Collections.emptyList().sort() does nothing

Alan Bateman Alan.Bateman at oracle.com
Wed Nov 15 11:18:29 UTC 2017


On 15/11/2017 10:03, Andrej Golovnin wrote:
> Hi Bernd,
>
> On Wed, Nov 15, 2017 at 7:04 AM, Bernd Eckenfels <ecki at zusammenkunft.net> wrote:
>> I would however vote for allowing an empty list to be sorted. This is such a common case to return a replacement empty list that it will not only introduce changed behavior but also forces ugly code.
> 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.
>
>
There are indeed inconsistencies and likely other examples due to List 
implementations not overriding sort (and so dependent on the default 
implementation which cannot say if an empty or already sorted list is 
modifiable or not). As Collections.emptyList() is around a long time 
then it would likely break existing code to change it now. It might be 
saner to change List0 to be consistent. In any case, the sort javadoc 
will likely need some adjustment to allow for long standing behavior.

-Alan


More information about the core-libs-dev mailing list