JDK-8159527 : Collections mutator methods should all be marked as optional operations
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2016-06-14
  • Updated: 2023-08-10
  • Resolved: 2023-08-04
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 22
22 b10Fixed
Related Reports
CSR :  
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Description
In order to reduce interface proliferation, some of the methods on the collection interfaces are designated as optional operations. Most of these are mutator methods.

Unfortunately, some of the mutator methods that were added recently, most notably the default methods added in Java 8, are not specified to be optional. In fact, they are optional, and they'll throw UnsupportedOperationException when called on an unmodifiable collection. Strictly speaking, then, this is a violation of the specification. The specifications of such methods should be modified to say that they are optional operations, and UnsupportedOperationException should be specified to be thrown if the implementing collection doesn't support that operation.

The list of methods that need to be updated is as follows:

Collection.removeIf
List.replaceAll
List.sort
NavigableSet.pollFirst
NavigableSet.pollLast

Map.compute
Map.computeIfAbsent
Map.computeIfPresent
Map.merge
Map.putIfAbsent
Map.remove (2-arg)
Map.replace (2-arg)
Map.replace (3-arg)
Map.replaceAll
NavigableMap.pollFirstEntry
NavigableMap.pollLastEntry

Most cases simply need "(optional operation)" to be added to the end of the leading sentence fragment in the specification.

In a few cases, the doc for UnsupportedOperationException should be augmented to allow UOE to be thrown if the implementation doesn't support the operation in general.

Navigable{Map,Set}.poll* need the most work.
Comments
Changeset: b2add96c Author: Stuart Marks <smarks@openjdk.org> Date: 2023-08-04 19:27:56 +0000 URL: https://git.openjdk.org/jdk/commit/b2add96c353f15b91524e10207e49841298bed01
04-08-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/15127 Date: 2023-08-02 20:11:35 +0000
02-08-2023

In addition, an anchor "optional-operation" should be added at the appropriate paragraph to the Collection class specification, and occurrences of "(optional operation)" should be changed to links to that anchor. This is similar to the nearby optional-restrictions anchor and links to it. UPDATE 2023-08-02: This would add a lot of links and would be a fairly extensive change, so it should be handled separately. See JDK-8313644.
02-08-2023