Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between add(E e) and offer(E e) of ArrayDqueue Class

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();
}
like image 405
Sachin Sachdeva Avatar asked Mar 17 '15 05:03

Sachin Sachdeva


People also ask

What is the difference between ADD and offer?

The difference is that offer() will return false if it fails to insert the element on a size restricted Queue, whereas add() will throw an IllegalStateException .

What is the difference between ArrayDeque and LinkedList?

The ArrayDeque class is the resizeable array implementation of the Deque interface, whereas the LinkedList class is the list implementation. The basic insertion, removal and retrieval operations in the Deque interface addFirst , addLast , removeFirst , removeLast , getFirst and getLast .

What is difference ArrayList and ArrayDeque?

ArrayList is resized to a new size of oldCapacity + (oldCapacity >> 1) , resulting in an increse of ~50%. The default capacity is 10, resulting in a capacities after resize of 15, 22, 33, 49, 73, 109, 163, 244, 366... ArrayDeque is always resized to a power of 2. On resize, the capacity is doubled.

Can we add null in ArrayDeque?

Null elements are prohibited. This class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue. Most ArrayDeque operations run in amortized constant time.


2 Answers

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.

like image 138
ruakh Avatar answered Sep 22 '22 07:09

ruakh


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.

like image 37
Makoto Avatar answered Sep 22 '22 07:09

Makoto