Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 175913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:47:00+00:00 2026-05-11T13:47:00+00:00

Say I have a java PriorityQueue (which java implements as a heap) that I

  • 0

Say I have a java PriorityQueue (which java implements as a heap) that I iterate over to remove elements based on some criteria:

PriorityQueue q = new PriorityQueue(); ... Iterator it = q.iterator(); while(it.hasNext()){     if( someCriterion(it.next()) )         it.remove(); } 

How long does each remove() operation take? I’m not sure whether it’s O(log(n)) or O(1).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:47:01+00:00Added an answer on May 11, 2026 at 1:47 pm

    If you’re using the Sun implementation, it’s O(log(n)). From the Javadocs:

    Implementation note: this implementation provides O(log(n)) time for the enqueing and dequeing methods (offer, poll, remove() and add); linear time for the remove(Object) and contains(Object) methods; and constant time for the retrieval methods (peek, element, and size).

    Other implementations could have different complexity.


    Edit: 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 here; it is also included in the JDK, so you might already have it installed.

    In the PriorityQueue‘s iterator, the default case for remove() is to call PriorityQueue.removeAt(lastRet), where lastRet is the index that was last returned by next(). removeAt() appears to be O(log(n)) worst case (it might have to sift the queue, but doesn’t have to iterate).

    However, sometimes bad things happen. From the comments of removeAt():

    /**  * 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.  */ 

    When a non-null element is returned by removeAt(), 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 remove() is called during this second phase of iteration, the iterator calls PriorityQueue.removeEq(lastRetElt), where lastRetElt is the last element returned from the special queue. removeEq is forced to use a linear search to find the correct element to remove, which makes it O(n). BUT it can check elements using == rather than .equals(), so its constant factor is lower than PriorityQueue.remove(Object).

    So, in other words, removing with an iterator is technically O(n), but in practice it should be quite a bit faster than remove(Object).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 146k
  • Answers 146k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer this might help you understand it May 12, 2026 at 9:05 am
  • Editorial Team
    Editorial Team added an answer The dialog that is built in is the standard File… May 12, 2026 at 9:05 am
  • Editorial Team
    Editorial Team added an answer Simply because IE6 still represents 27.21% of the web's population… May 12, 2026 at 9:05 am

Related Questions

Let's say I have a java program that makes an HTTP request on a
Let's say I have a Java ArrayList, that is sorted. Now I would like
Let's say I have a java.util.Properties object. The Properties object has a method called
Using google's Protocul Buffers , I have a service already written in Java which

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.