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();
}
1
  • check doubleCapacity() method and also documentation in Deque Interface.
    – Prashant
    Commented Mar 17, 2015 at 6:29

4 Answers 4

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.

2
  • This is my question ..Sir what is the difference then since there is no size limit restriction.
    – Joker
    Commented Mar 17, 2015 at 6:06
  • 2
    @ShowStopper: As I wrote in this answer, the two methods are equivalent; there is no difference.
    – ruakh
    Commented Mar 17, 2015 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.

4
  • java.util.ArrayDequeue, , does not implement any capacity restrictions.
    – Joker
    Commented Mar 17, 2015 at 6:07
  • I did say that it had the capability. I never said that ArrayDeque actually would.
    – Makoto
    Commented Mar 17, 2015 at 6:07
  • Both are using same method. Kindly check the implementation.
    – Joker
    Commented Mar 17, 2015 at 6: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 the interface, 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
    Commented Mar 17, 2015 at 6: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).

0
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.

8
  • 1
    No. Both methods call addLast() and both throw IllegalStateException.
    – Eng.Fouad
    Commented Mar 17, 2015 at 5:59
  • @Eng.Fouad: No. offer does not explicitly throw an IllegalStateException.
    – Makoto
    Commented Mar 17, 2015 at 5:59
  • @Eng.Fouad see the Java doc docs.oracle.com/javase/7/docs/api/java/util/Deque.html#offer(E)
    – Prashant
    Commented Mar 17, 2015 at 6:00
  • 1
    @Makoto This is what I can see at code-level. Both have the same implementation.
    – Eng.Fouad
    Commented Mar 17, 2015 at 6:02
  • 1
    I won't disagree that the code is not going to throw it, but the guarantee by the interface says that add has the ability to throw the exception, whereas offer does not.
    – Makoto
    Commented Mar 17, 2015 at 6:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.