Collections.emptyList().sort() does nothing
Vitaly Davidovich
vitalyd at gmail.com
Wed Nov 15 11:18:59 UTC 2017
On Wed, Nov 15, 2017 at 5:21 AM Andrej Golovnin <andrej.golovnin at gmail.com>
wrote:
> > 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.
One can argue that since those are empty by construction/design, sorting
them is not a bug. That’s different from, say, trying to sort an
unmodifiable list that happens to be empty and not failing in that case.
>From a user’s perspective, I agree it’s annoying to have to special case
these. This is particularly so because immutability is not expressed in
the types or type system.
>
>
> >
> > --
> > Andrew Haley
> > Java Platform Lead Engineer
> > Red Hat UK Ltd. <https://www.redhat.com>
> > EAC8 43EB D3EF DB98 C
> <https://maps.google.com/?q=EF+DB98+C&entry=gmail&source=g>C77 2FAD A5CD
> 6035 332F A671
>
--
Sent from my phone
More information about the core-libs-dev
mailing list