On Mon, 19 Oct 2026 18:09:39 GMT, Viktor Klang <[email protected]> wrote:

>> Srikanth Palutla has updated the pull request incrementally with one 
>> additional commit since the last revision:
>> 
>>   Move singleton Comparable check into initElementsFromCollection
>
> src/java.base/share/classes/java/util/PriorityQueue.java line 304:
> 
>> 302:             Comparable<? super E> e = (Comparable<? super E>) es[0];
>> 303:             e.compareTo((E) es[0]);
>> 304:         }
> 
> Wouldn't it be sufficient to say:
> 
> if (len == 1 && this.comparator == null && !(es[0] instanceof Comparable))
>     throw new ClassCastException("class " + es[0].getClass().getName() + " 
> cannot be cast to " + Comparable.getClass().getName());

Understood, the instanceof check is cleaner and avoids invoking compareTo 
during construction. 

I'd originally mirrored TreeMap's self-comparison pattern (JDK-5045147), but 
the direct check expresses the intent better here. One fix to the snippet: 
Comparable.getClass().getName() evaluates to "java.lang.Class", so I've used 
Comparable.class.getName(). Updated and pushed the changes

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

PR Review Comment: https://git.openjdk.org/jdk/pull/32966#discussion_r4317723300

Reply via email to