The method add
is defined as
public boolean add(E e) {
return offer(e);
}
The method offer
is defined as
public boolean offer(E e) {
if (e == null)
throw new NullPointerException();
modCount++;
int i = size;
if (i >= queue.length)
grow(i + 1);
size = i + 1;
if (i == 0)
queue[0] = e;
else
siftUp(i, e);
return true;
}
So the answer to your question is yes. This current implementation always returns true
unless an exception is thrown.
The contract of Collection#add
says the method returns "true
if this collection changed as a result of the call". Since PriorityQueue
isn't a final class and it may have children, we can't say "it will never return false
". I may implement a bounded PriorityQueue
. I may come up with a PriorityQueue
that rejects particular values or doesn't accept new values at all.
PriorityQueue<Integer> queue = new PriorityQueue<Integer>() {
@Override
public boolean add(Integer integer) {
super.add(integer);
return false;
}
};