JDK-8267452 : Delegate forEachRemaining in Spliterators.iterator()
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2021-05-20
  • Updated: 2021-06-07
  • Resolved: 2021-05-26
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 17
17 b24Fixed
Related Reports
Relates :  
Description
Sometimes, Spliterator.forEachRemaining has much more optimized implementation, compared to Spliterator.tryAdvance. For example, if a Stream.spliterator() called for a stream that has intermediate operations, tryAdvance() may buffer intermediate elements while forEachRemaining() just delegates to the wrapAndCopyInto (see StreamSpliterators.AbstractWrappingSpliterator and its inheritors).

Spliterators.iterator() methods (used in particular by Stream.iterator()) provide adapter iterators that delegate to the supplied spliterator. Unfortunately, they don't have a specialized implementation for Iterator::forEachRemaining. As a result, a default implementation is used that delegates to hasNext/next, which in turn causes the delegation to tryAdvance. It's quite simple to implement Iterator::forEachRemaining here, taking advantage of possible spliterator optimizations if the iterator client decides to use forEachRemaining.
Comments
Changeset: ac36b7d3 Author: Tagir F. Valeev <tvaleev@openjdk.org> Date: 2021-05-26 01:17:02 +0000 URL: https://git.openjdk.java.net/jdk/commit/ac36b7d3e2d521652576fba3b1760586f582544f
26-05-2021