tags:

views:

218

answers:

3

Take the PriorityQueue for example http://java.sun.com/j2se/1.5.0/docs/api/java/util/PriorityQueue.html#offer(E)

Can anyone give me an example of a queue where the add and offer methods are different?

According to the Collection API entry http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html the add method will often seek to ensure that an element exists within the Collection rather than adding duplicates. So my question is, what is the difference between the add and offer methods?

Is it that the Offer method will add duplicates regardless? (I doubt that it is because if a Collection should only have distinct elements this would circumvent that).

EDIT: In a PriorityQueue the add and offer methods are the same method (see my answer below). Can anyone give me an example of a class where the add and offer methods are different?

+1  A: 

There is no difference for the implementation of PriorityQueue.add:

public boolean add(E e) {
    return offer(e);
}

For AbstractQueue there actually is a difference:

public boolean add(E e) {
    if (offer(e))
        return true;
    else
        throw new IllegalStateException("Queue full");
}
Peter Lang
I know, I just posted that answer myself a few minutes ago. Do you know of any classes where the `add` method is different to the `offer` method?
Finbarr
@Finbarr: Yes, check my updated answer
Peter Lang
+2  A: 

I guess the difference is in the contract, that when element can not be added to collection the add method throws an exception and offer does'nt.

From: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html#add%28E%29

If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false). This preserves the invariant that a collection always contains the specified element after this call returns.

From: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Queue.html#offer%28E%29

Inserts the specified element into this queue, if possible. When using queues that may impose insertion restrictions (for example capacity bounds), method offer is generally preferable to method Collection.add(E), which can fail to insert an element only by throwing an exception.

dvd
+1 for finding that snippet about when to use `offer` vs `add`.
Finbarr
+1  A: 

The difference between offer and add is explained by these two excerpts from the javadocs:

From the Collection interface:

If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false). This preserves the invariant that a collection always contains the specified element after this call returns.

From the Queue interface

When using queues that may impose insertion restrictions (for example capacity bounds), method offer is generally preferable to method Collection.add(E), which can fail to insert an element only by throwing an exception.

PriorityQueue is a Queue implementation that does not impose any insertion restrictions. Therefore the add and offer methods have the same semantics.

By contrast, ArrayBlockingQueue is an implementation in which offer and add behave differently, depending on how the queue was instantiated.

Stephen C