On Wed, 8 Jul 2026 19:41:54 GMT, Daisuke Yamazaki <[email protected]> wrote:
>> src/java.base/share/classes/java/util/PriorityQueue.java line 381: >> >>> 379: return false; >>> 380: >>> 381: Object[] es = prepareElements(c, comparator); >> >> What's the purpose of calling `prepareElements` here? We know `c` is exactly >> PriorityQueue, and PriorityQueue does not allow `null`-elements? >> >> I think `initElementsFromArray` can be removed, and its logic embedded >> directly into `addAll` and `initElementsFromCollection`, and if >> `prepareElements` isn't needed in `addAll` then `prepareElements` can be >> removed as a method and its logic be moved directly into >> `initElementsFromCollection`. >> >> Am I missing something? 🤔 > > Thanks, I think there may be a small confusion here. > > The exact `PriorityQueue` check is only for the receiver. > The source collection `c` can still be any `Collection`, so `prepareElements` > is still needed there. > > I agree with the refactoring suggestion, though. I will fold the > `initElementsFromArray` logic into the callers and remove that helper. Ah, yes, good point. So that means that if we check the exact type of `c` then we can trust its `isEmpty()` (need to amend that) and as well not have to check for null elements. So adding a Pq to a PQ should then be as optimal as can be. ------------- PR Review Comment: https://git.openjdk.org/jdk/pull/31701#discussion_r3547301042
