JDK-8214687 : Optimize Collections.nCopies().hashCode() and equals()
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 8,11,13
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2018-12-03
  • Updated: 2019-10-04
  • Resolved: 2018-12-30
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 11 JDK 13 JDK 7 JDK 8 Other
11.0.5-oracleFixed 13 b02Fixed 7u241Fixed 8u231Fixed openjdk8u232Fixed
Currently Collections.nCopies().hashCode() delegates to AbstractList.hashCode() which creates an iterator and caculates the hashCode of the same element n times. An optimized implementation is possible which:

- Doesn't create an iterator
- Calls element.hashCode() at most once
- Has O(log(n)) complexity

Also specialized implementation of CopiesList.equals() is possible which at least can check whether another object is also a CopiesList (in this case, no iteration is necessary, just compare single element and size).
Fix Request (11u) Backporting this patch keeps codebases in sync (I see 11.0.5-oracle). Patch applies cleanly to 11u, passes tier1 tests.

Fix Request (8u) Backporting this performance fix keeps codebases in sync (I see 8u231). Patch applies with usual reshufflings, and requires touchups in test code to compile and run. Passes tier1-like suite afterwards. 8u RFR: https://mail.openjdk.java.net/pipermail/jdk8u-dev/2019-July/009757.html