Consider the following code:
import java.util.PriorityQueue; public class Test { public static void main(String argv[]) { PriorityQueue<A> queue = new PriorityQueue<>(); System.out.println("Size of queue is " + queue.size());
In this code, an object that is clearly not comparable is added to the PriorityQueue . As expected in the PriorityQueue.add Javadoc, this code throws a ClassCastException because the object is not comparable.
However, it seems the queue size is still increasing, although an exception has been thrown.
I would expect both print statements to print 0, and the second actually prints 1, as if the object was added to the queue.
What's going on here?
source share