RFR: 8368178: Add specialization of SequencedCollection methods to emptyList, singletonList and nCopies [v2]

Tagir F. Valeev tvaleev at openjdk.org
Mon Sep 22 08:09:17 UTC 2025


On Mon, 22 Sep 2025 07:22:15 GMT, Per Minborg <pminborg at openjdk.org> wrote:

>> @minborg `EmptyList` already implements it, as any other `List`:
>> 
>> public interface List<E> extends SequencedCollection<E> { ... }
>> 
>> 
>> For `EmptyMap` (and even `SingletonMap` and `SingletonSet`), it's possible, but it will require changing the public interface (`Collections::emptyMap` will have to return `SequencedMap`), which may produce binary compatibility issues. Probably we can invent a binary-compatible signature like this:
>> 
>> 
>> public static final <K,V,M extends Map<K, V> & SequencedMap<K, V>> M emptyMap() { ... }
>> 
>> But it looks ugly.
>
> @amaembo `SequencedMap` already implements `Map`.  :-) So, we could say:
> 
> 
> public static final <K,V> SequencedMap<K, V> emptyMap() { ... }
> 
> 
> An empty map could also "incidentally" implement `SequencedMap`.

@minborg as JLS 13.4.15 says,
> Changing the result type of a method, or replacing a result type with void, or replacing void with a result type, has the combined effect of deleting the old method and adding a new method with the new result type or newly void result

Changing the result type from `Map` to `SequencedMap` will modify the binary signature, so compiled classes that used this method previously will fail with `NoSuchMethodError`. That's why simply changing `Map` to `SequencedMap` is not quite possible. My trick with `M extends Map<K, V> & SequencedMap<K, V>` employs the fact that the type parameter gets erased to its first bound, which is `Map`, so such kind of return type replacement is safe (but ugly).

-------------

PR Comment: https://git.openjdk.org/jdk/pull/27406#issuecomment-3317469167


More information about the core-libs-dev mailing list