On Sun, 18 Oct 2026 05:27:40 GMT, Srikanth Palutla <[email protected]> wrote:

>> Problem
>> new PriorityQueue<>(List.of(new Object())) succeeds today when it should 
>> throw ClassCastException. initFromCollection relies on heapify() to surface 
>> incomparable elements, but heapify performs no comparisons on a singleton, 
>> so the lone element is never validated. Collections with two or more 
>> non-Comparable elements already throw.
>> 
>> Fix
>> In initFromCollection, when the collection holds exactly one element and no 
>> comparator is in play, self-compare the element before heapifying. This 
>> mirrors what TreeMap does for its first key (see JDK-5045147). Behavior with 
>> an explicit comparator is unchanged: non-Comparable elements are still 
>> accepted when a comparator is provided.
>> 
>> Testing
>> Added a copyConstructorSingleton regression test to AddNonComparable 
>> covering PriorityQueue(Collection) and PriorityQueue(Collection, Comparator) 
>> with a null comparator, plus the positive cases (Comparable singleton, 
>> explicit comparator). Verified the new test passes against the patched class 
>> and the existing queues() test still passes.
>> 
>> 
>> Please let me know for further questions 
>> 
>> 
>> ---------
>> - [x] I confirm that I make this contribution in accordance with the 
>> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai).
>
> Friendly ping — this has been awaiting review since September. Quick recap: 
> PriorityQueue.initFromCollection never validates a single-element collection 
> (heapify performs no comparisons on a singleton), so new 
> PriorityQueue<>(List.of(new Object())) succeeds where it should throw 
> ClassCastException. The fix self-compares the lone element when no comparator 
> is set, mirroring TreeMap's first-key check (JDK-5045147). All automated 
> checks are green; it just needs a Reviewer.
> 
> @viktorklang-ora @liach — you both reviewed the recent PriorityQueue 
> constructor work in #31452; if either of you has a few minutes, a review here 
> would be much appreciated.

> Hello @sripal2013, reading through some of the comments in this PR, to me, it 
> sounds like they may have been generated using LLM. Can you confirm whether 
> or not that's the case? The OpenJDK LLM policy https://openjdk.org/legal/ai 
> applies even for PR comments.

Hello @jaikiran thanks for pointing it out . The analysis behind the replies is 
by me. However, I am not a native English speaker, to explain the changes 
better, I had to use AI. I was not aware of the comments policy. I had updated 
the comments.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/32966#issuecomment-6281086397

Reply via email to