On Fri, 15 May 2026 19:19:26 GMT, Stuart Marks <[email protected]> wrote:
> Change the following collections to be strictly unmodifiable:
>
> - The reversed view of Collections.unmodifiableList()
> - The reversed view Collections.unmodifiableSortedSet()
>
> "Strictly unmodifiable" means that, if a mutator method is called that
> doesn't actually attempt to modify the collection, then
> UnsupportedOperationException will be thrown instead of the method returning
> normally. For example, consider the following:
>
> Collections.unmodifiableList(new ArrayList<>()).reversed().remove("x")
> Collections.unmodifiableSortedSet(new TreeSet<>()).reversed().removeIf(x
> -> false)
>
> Both of these currently return false. With this change, both will throw
> UnsupportedOperationException.
>
> This improves consistency of behavior of reversed views, which were all
> intended to be strictly unmodifiable if the backing collection is
> unmodifiable. This also improves consistency of the tests, which had some
> cases commented out because of these edge cases.
>
> See bug [JDK-8383625](https://bugs.openjdk.org/browse/JDK-8383625) for full
> discussion.
>
> ---------
> - [x] I confirm that I make this contribution in accordance with the [OpenJDK
> Interim AI Policy](https://openjdk.org/legal/ai).
Minor behavior change, but it reduces the set of allowed behaviors, so it
should be documented.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/31177#issuecomment-4462767126