سؤال

I was going though FIFO implementation in Java and came across this java.util.Queue interface. Dequeue implements it which in turn is implemented by Linked List.

I wrote the following code

public class FIFOTest {

    public static void main(String args[]){

        Queue<String> myQueue = new LinkedList<String>();
        myQueue.add("US");
        myQueue.offer("Canada");

        for(String element : myQueue){
            System.out.println("Element : " + element);
        }
    }

}

Both seem to do the same thing. Add data to the head of the queue. What is the difference between these two methods? Any special cases in which either would be more beneficial than other?

هل كانت مفيدة؟

المحلول

LinkedList#offer(E) is implemented as

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

In this case, they are the same thing. They are just needed to satisfy the interfaces. LinkedList implements Deque and List. The LinkedList#add(E) method will not throw an Exception as it will always take more elements, but in another Queue implementation that has limited capacity or takes only certain kinds of elements, add(E) might throw an exception while offer(E) will simply return false.

نصائح أخرى

According to the docs the main difference is that when the operation fails, one (add) throws an exception and the other (offer) returns a special value (false):

Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation). The latter form of the insert operation is designed specifically for use with capacity-restricted Queue implementations; in most implementations, insert operations cannot fail.

What is the difference between these two methods?

  • Queue.add - throws an exception if the operation fails,
  • Queue.offer- returns a special value (either null or false, depending on the operation).

Any special cases in which either would be more beneficial than other?

According to docs, The Queue.offer form of the insert operation is designed specifically for use with capacity-restricted Queue implementations; in most implementations, insert operations cannot fail.

For details, read this docs.

  • add() comes from Collection Interface.
  • offer() comes from Queue Interface.

The Documentation of offer() method of Queue says

  Inserts the specified element into this queue if it is possible to do
  so immediately without violating capacity restrictions.
  When using a capacity-restricted queue, this method is generally
  preferable to {@link #add}, which can fail to insert an element only
  by throwing an exception.

The Documentation of add() method of Queue says

 Inserts the specified element into this queue if it is possible to do so
 immediately without violating capacity restrictions, returning
 <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
 if no space is currently available.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top