Collections.emptyList().sort() does nothing

Stuart Marks stuart.marks at oracle.com
Thu Nov 16 06:35:15 UTC 2017



On 11/14/17 7:52 PM, Tagir Valeev wrote:
> 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".
> ...
> To me it seems that emptyList and (especially) singletonList
> implementation does not follow the specification. Am I missing
> something?

Hi Tagir,

This is indeed an inconsistency. As Martin noted, different maintainers of the 
collections framework took different approaches on how strict to be in these cases.

Note also that

     Collections.emptyList().addAll(List.of())

is a no-op, whereas

     Collections.unmodifiableList(new ArrayList<String>()).addAll(List.of())
     List.of().addAll(List.of())

both throw UnsupportedOperationException.

The issue is whether a collection that considers itself unmodifiable allows 
mutator methods to be called, if the operation wouldn't actually make a change 
(lenient); or if it unconditionally throws UOE even if the operation wouldn't 
actually make a change (strict).

The Collections.unmodifiableX implementations are strict, as are the new 
List.of/Set.of/Map.of implementations I added in JDK 9.

As you've noticed, certain special-case collections like Collections.emptyList() 
and singletonList() are described as "immutable" (really, unmodifiable), but 
they are lenient. While this isn't guaranteed by the spec, changing these at 
this point would be a behavioral incompatibility, so we're unlikely ever to 
change them.

My view is that it's preferable for collections implementations to be strict. If 
I'm handing out an unmodifiable List, it should always be illegal for the callee 
to attempt to sort it, even if it has zero or one elements.

s'marks



More information about the core-libs-dev mailing list