9

Hi I used add and offer to add my element in last pace. Both are returning boolean and both does not throw any exception apart from NPE.

 public class ArrayDequeDemo {

  public static void main(String[] args) {


    // Create ArrayDeque elements.
    ArrayDeque<Integer> deque = new ArrayDeque<>();
    deque.add(10);
    deque.offer(30);

   }
 }

Both will add element in last place by returning a boolean.

JAVA IMPLEMENTATION

//For Add and Offer Both
   public void addLast(E e) {
    if (e == null)
        throw new NullPointerException();
    elements[tail] = e;
    if ( (tail = (tail + 1) & (elements.length - 1)) == head)
        doubleCapacity();
}
T-Bag
  • 10,916
  • 3
  • 54
  • 118

4 Answers4

15

The two methods are equivalent.

The reason that they both exist is that the java.util.Queue interface specifies both.

The reason that java.util.Queue specifies both is that an implementation of java.util.Queue is allowed to implement capacity restrictions, and the two methods are specified to behave differently in the case that adding the element would violate that restriction; specifically, add(...) is specified to throw IllegalStateException in that case, whereas offer(...) simply returns false.

java.util.ArrayDeque, however, does not implement any capacity restrictions, so this situation does not arise with it, so the distinction does not apply.

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • This is my question ..Sir what is the difference then since there is no size limit restriction. – T-Bag Mar 17 '15 at 06:06
  • 2
    @ShowStopper: As I wrote in this answer, the two methods are equivalent; there *is* no difference. – ruakh Mar 17 '15 at 15:15
5

The Queue documentation does a fairly good job of explaining the difference.

  • add(E e) has the capability of throwing an exception if an element can't be added into the queue. This happens in case the queue is full.

  • offer(E e) will return a special value (in this case, a boolean) if the value can't be added into the queue. This is useful if you're dealing with a size-limited queue but do not want to throw an exception.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • java.util.ArrayDequeue, , does not implement any capacity restrictions. – T-Bag Mar 17 '15 at 06:07
  • I *did* say that it had the *capability*. I never said that `ArrayDeque` actually *would*. – Makoto Mar 17 '15 at 06:07
  • Both are using same method. Kindly check the implementation. – T-Bag Mar 17 '15 at 06:12
  • 2
    I'm explaining the actual interface as opposed to the underlying implementation. This is a *fair* stance to take as one could easily swap out the interface for something else. As both methods are [specified by](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayDeque.html#add(E)) [the interface](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayDeque.html#offer(E)), there is nothing incorrect about mentioning the interface. I am well aware that `ArrayDeque` won't throw an exception, but it **is** specified by that interface. – Makoto Mar 17 '15 at 06:17
2

In Java SE 8

  • boolean add(E e)

    Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
    When using a capacity-restricted deque, it is generally preferable to use offer(E e).

    Inserts the specified element at the end of this deque.
    This method is equivalent to addLast(E).

  • boolean offer(E e)

    Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.
    When using a capacity-restricted deque, this method is generally preferable to the add(E) method, which can fail to insert an element only by throwing an exception.

    Inserts the specified element at the end of this deque.
    This method is equivalent to offerLast(E).

1

offer(E e) method will not throw IllegalStateException if no space is currently available it will return false if no free space available

but add(E e) method will throw IllegalStateException if no space is currently available.

and other things are same.

Prashant
  • 2,556
  • 2
  • 20
  • 26