Collections.emptyList().sort() does nothing

Tagir Valeev amaembo at
Wed Nov 15 03:52:27 UTC 2017


According to `List.sort` specification [1] "This list must be modifiable".
According to `Collections.emptyList` specification [2] "Returns an
empty list (immutable)."

So I assume that `List.sort` cannot be called on
`Collections.emptyList` result. However in fact this call is allowed
doing nothing. Is this behavior documented somehow? Can I rely that it
will not change in future Java updates?

It's even more strange with `Collections.singletonList` [3] which
"Returns an immutable list containing only the specified object".
Obviously I cannot do:

List<String> list = Collections.singletonList("foo");
ListIterator<String> it = list.listIterator();;
it.set("bar"); // UOE

However list.sort(null) works perfectly. On the other hand [1] clearly says:

Throws: UnsupportedOperationException - if the list's list-iterator
does not support the set operation.

To me it seems that emptyList and (especially) singletonList
implementation does not follow the specification. Am I missing

With best regards,
Tagir Valeev.


More information about the core-libs-dev mailing list