offer(E e) method of Deque Interface in Java.

offer(E e) method of Deque Interface in Java.


Posted in : Core Java Posted on : February 5, 2011 at 4:51 PM Comments : [ 0 ]

boolean offer(E e) This method inserts an element at bottom into a deque and returns 'true' if element is added to a deque otherwise returns false.

offer(E e) method of Deque Interface in Java.

In this section we will discuss how can offer(E e) method be implemented in Deque interface in java.

Syntax

boolean offer(E e)

This method inserts an element at bottom into a deque and returns 'true' if element is added to a deque otherwise returns false.

In this method you can insert a specified element at last position into deque. This method is preferably used when we are restricted with the capacity of deque. It adds an element to the underlying deque if it is possible to do so instantly without breaking the capacity limitation of the deque. The add(E e) method is different from this method only by their behavior, the offer(E e) method throws exceptions as well as returns 'false' if insertion of an element in deque gets fail whereas add(E e) method can only throws an exception.

Parameter description

e : It takes an element what do you want to insert at last position into deque.

Example of offer(E e) method 

In this example we will show you how does an offer (E e) method work in Deque interface. In the following example we will show how you can add a specified element at the last position into Deque, and count the total number of elements of deque before and after adding the elements and the returned value of this method depending upon the operation.

 Example :

package devmanuals.com;
import java.util.Deque;
import java.util.LinkedList;
public class DequeOfferElement {
  public static void main(String[] args) {
    Deque<String> dq = new LinkedList<String>();
    System.out.println("Initially the size of deque is " + dq.size());
    System.out.println("Elements in deque : " + dq);
    dq.offer("Ram");
    dq.offer("Shyam");
    dq.offer("Radhey Shayam");
    System.out.println("After addition of elements into deque, the elements are : "+ dq);
    System.out.println("And the size of deque : " + dq.size());
    System.out.println("Now insert a new element 'JaiRam'");
    boolean b = dq.offer("Jai Ram");
    System.out.println("Then the new Elements of deque are : " + dq);
    System.out.println("Inserts the specified element : " + b);
    System.out.println("And the size of new deque = " + dq.size());
  }
}

Output :

Initially the size of deque is 0

Elements in deque : []

After addition of elements into deque, the elements are :  
       [Ram, Shyam, Radhey Shayam]

And the size of deque : 3

Now insert a new element 'JaiRam'

Then the new Elements of deque are :
       [Ram, Shyam, Radhey Shayam, Jai Ram]

Inserts the specified element : true

And the size of new deque = 4 

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics