2

I was wondering since the API says that the remove() function returns a boolean type, how do I grab the element's info when I remove it. That is, is it possible to do:

class Node {
  int stuff;
}

PriorityQueue <Node> nodes = new PriorityQueue <Node> ();

Node temp = nodes.remove(0);

Assuming off course, that the nodes PQ has a bunch of nodes in it. Would temp node have the information of the removed node or would that not work since remove() returns a boolean

2
  • 1
    What is it you're trying to accomplish exactly? You're probably using the wrong Collection for the job. Commented Sep 21, 2011 at 11:35
  • remove() returns a Node. remove(Object o) returns a boolean. You are using the wrong remove function. remove(int index) is part of List, not Queue. Commented Oct 22, 2011 at 2:27

4 Answers 4

4

Use poll method to retrieve head element

Sign up to request clarification or add additional context in comments.

3 Comments

poll() is no replacement for what he wants since it only returns the head. Whereas remove(Object o) deletes the object no matter the position in the queue.
@Psy, I understand. I just thought that there's no point to 'grab the element's info' while removing it. I think OP just want to retrieve queue's head element. Anyway, I just made a link to documentation and didn't claim that this will solve all of the problems.
@Psy yeah my bad, I am trying to get the head of the queue
3

In a Queue, you can't remove Objects by position remove(int), you can only remove by reference remove(E). Queues aren't designed for random access. (The only exception probably being LinkedList, as that implements both the List and Queue interface). Your code does not compile.

2 Comments

but what if I know the exact position i of the element and want to remove it constant time but not in O(n)?
@mvb13 then you'll need a Queue implementation that provides random access efficiently. The jdk has no such Queue, but there are some high-performance collection libraries. Perhaps one of them has such a beast. Happy hunting!
0

This is where it is best to read the documentation. remove(object) removes that object from the queue. Due to backward compatibility its not smart enough to tell you there is no Integers in your queue. i.e. you are trying to remove the object (Integer) 0

Instead you should use the remove() method which takes no arguments which

Retrieves and removes the head of this queue. This method differs from poll only in that it throws an exception if this queue is empty.

However, a better choice might be to use poll as it does the same thing except it return null instead of an exception when the queue is empty.

3 Comments

poll() is no replacement for what he wants since it only returns the head. Whereas remove(Object o) deletes the object no matter the position in the queue.
Assumption is the mother of all... ;-)
Yes, I like your comment at the top. Its just as likely. esp as the Node doesn't extend Comparable.
0

That wouldn't work, you'll end up with a compile error because you're trying to assign the primitive type boolean to a variable that contains a reference to a Node instance. If you can access the Nodes in your PriorityQueue by index, you can save a reference to the object in whichever index you want first, and then remove it.

That said, looking briefly at the API for the PriorityQueue class, it looks like your usage of remove() is incorrect as well (it takes an object, not an int).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.