Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strike>If you're using the Sun implementation, it's <code>O(log(n))</code>.</strike> From the <a href="http://java.sun.com/javase/6/docs/api/java/util/PriorityQueue.html" rel="noreferrer">Javadocs</a>:</p> <blockquote> <p>Implementation note: this implementation provides O(log(n)) time for the enqueing and dequeing methods (<code>offer</code>, <code>poll</code>, <code>remove()</code> and <code>add</code>); linear time for the <code>remove(Object)</code> and <code>contains(Object)</code> methods; and constant time for the retrieval methods (<code>peek</code>, <code>element</code>, and <code>size</code>).</p> </blockquote> <p>Other implementations could have different complexity.</p> <hr> <p><strong>Edit:</strong> The Javadocs don't cover the performance of removing an element with an iterator, so I had to look up the source code. This is all relevant to the Sun implementation, and may differ in Apple's version, GNU Classpath, etc. Sun's source is available <a href="http://download.java.net/jdk6/" rel="noreferrer">here</a>; it is also included in the JDK, so you might already have it installed.</p> <p>In the <code>PriorityQueue</code>'s iterator, the default case for <code>remove()</code> is to call <code>PriorityQueue.removeAt(lastRet)</code>, where <code>lastRet</code> is the index that was last returned by <code>next()</code>. <code>removeAt()</code> appears to be <code>O(log(n))</code> worst case (it might have to sift the queue, but doesn't have to iterate).</p> <p>However, sometimes bad things happen. From the comments of <code>removeAt()</code>:</p> <pre><code>/** * Removes the ith element from queue. * * Normally this method leaves the elements at up to i-1, * inclusive, untouched. Under these circumstances, it returns * null. Occasionally, in order to maintain the heap invariant, * it must swap a later element of the list with one earlier than * i. Under these circumstances, this method returns the element * that was previously at the end of the list and is now at some * position before i. This fact is used by iterator.remove so as to * avoid missing traversing elements. */ </code></pre> <p>When a non-null element is returned by <code>removeAt()</code>, the iterator adds it to a special queue for later use: when the iterator runs out of elements in the queue, it then iterates through this special queue. When <code>remove()</code> is called during this second phase of iteration, the iterator calls <code>PriorityQueue.removeEq(lastRetElt)</code>, where <code>lastRetElt</code> is the last element returned from the special queue. <code>removeEq</code> is forced to use a linear search to find the correct element to remove, which makes it <code>O(n)</code>. BUT it can check elements using <code>==</code> rather than <code>.equals()</code>, so its constant factor is lower than <code>PriorityQueue.remove(Object)</code>.</p> <p>So, in other words, removing with an iterator is technically <code>O(n)</code>, but in practice it should be quite a bit faster than <code>remove(Object)</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload