What happens to my PriorityQueue if my Comparator throws an exception while it's busy bubbling up or

Posted by nieldw on Stack Overflow See other posts from Stack Overflow or by nieldw
Published on 2010-04-09T10:11:28Z Indexed on 2010/04/09 10:13 UTC
Read the original article Hit count: 347

Filed under:
|
|

Hi,

I'm trying order pairs of integers ascendantly where a pair is considered less than another pair if both its entries are strictly less than those of the other pair, and larger than the other pair if both its entries are strictly larger than those of the other pair. All other cases are considered incomparable.

They way I want to solve this is by defining a Comparator that implements the above, but will throw an exception for incomparable cases, and provide that to a PriorityQueue. Of course, while inserting a pair the priority queue does several comparisons while bubbling the new entry up to its correct position in the heap, and many of these will be comparable. But it may happen during the bubbling process that a pair is encountered with which this new pair is incomparable, and an exception will be thrown. If this happens, what will be the state of the PriorityQueue? Will the pair I was trying to insert sit in the heap at the last position it was in before the exception was thrown? If I use the PriorityQueue's remove(Object o) method, will the PriorityQueue be restored to a consistent state?

Thanks

© Stack Overflow or respective owner

Related posts about java

Related posts about priority-queue