On Fri, 27 Jan 2023 13:53:45 GMT, Glavo <d...@openjdk.org> wrote: >> I checked the `java.base` module, and all the `Collection#toArray()` method >> of collections be implemented correctly. >> >> Their return values can be trusted, so many unnecessary array duplication >> can be eliminated. > > Glavo has updated the pull request incrementally with one additional commit > since the last revision: > > Collections.isTrustedCollection
What about creating a public `Collections.toArray(Collection c)` method that would return `c.toArray` for trusted collections, otherwise return a copy? This would allow any code to get a trustable array, while avoiding the copy when possible. Even if this method was packaged-scoped, it might be a more concise way to access this capability in most cases. ------------- PR: https://git.openjdk.org/jdk/pull/12212