Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Deque offer() method in Java

  • Last Updated : 21 Sep, 2018

The offer(E e) method of Deque Interface inserts the specified element into this Deque if it is possible to do so immediately without violating capacity restrictions. This method is preferable to add() method since this method does not throws an exception when the capacity of the container is full since it returns false.

Syntax:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

boolean offer(E e)

Parameters: This method accepts a mandatory parameter e which is the element to be inserted in the Deque.

Returns: This method returns true on successful insertion else it returns false.



Exceptions: The function throws four exceptions which are described as below:

  • ClassCastException: when the class of the element to be entered prevents it from being added to this container.
  • IllegalArgumentException: when some property of the element prevents it to be added to the Deque.
  • NullPointerException: when the element to be inserted is passed as null and the Deque’s interface does not allow null elements.

Below programs illustrate offer() method of Deque:

Program 1: With the help of LinkedList.




// Java Program Demonstrate offer()
// method of Deque when Null is passed
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Deque
        Deque<Integer> DQ
            = new LinkedList<Integer>();
  
        if (DQ.offer(10))
            System.out.println("The Deque is not full and 10 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(15))
            System.out.println("The Deque is not full and 15 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(25))
            System.out.println("The Deque is not full and 25 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(20))
            System.out.println("The Deque is not full and 20 is inserted");
        else
            System.out.println("The Deque is full");
  
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}
Output:
The Deque is not full and 10 is inserted
The Deque is not full and 15 is inserted
The Deque is not full and 25 is inserted
The Deque is not full and 20 is inserted
Deque: [10, 15, 25, 20]

Program 2: With the help of ArrayDeque.




// Java Program Demonstrate offer()
// method of Deque when Null is passed
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Deque
        Deque<Integer> DQ
            = new ArrayDeque<Integer>();
  
        if (DQ.offer(10))
            System.out.println("The Deque is not full and 10 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(15))
            System.out.println("The Deque is not full and 15 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(25))
            System.out.println("The Deque is not full and 25 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(20))
            System.out.println("The Deque is not full and 20 is inserted");
        else
            System.out.println("The Deque is full");
  
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}
Output:
The Deque is not full and 10 is inserted
The Deque is not full and 15 is inserted
The Deque is not full and 25 is inserted
The Deque is not full and 20 is inserted
Deque: [10, 15, 25, 20]

Program 3: With the help of ConcurrentLinkedDeque.




// Java Program Demonstrate offer()
// method of Deque when Null is passed
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Deque
        Deque<Integer> DQ
            = new ConcurrentLinkedDeque<Integer>();
  
        if (DQ.offer(10))
            System.out.println("The Deque is not full and 10 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(15))
            System.out.println("The Deque is not full and 15 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(25))
            System.out.println("The Deque is not full and 25 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(20))
            System.out.println("The Deque is not full and 20 is inserted");
        else
            System.out.println("The Deque is full");
  
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}
Output:



The Deque is not full and 10 is inserted
The Deque is not full and 15 is inserted
The Deque is not full and 25 is inserted
The Deque is not full and 20 is inserted
Deque: [10, 15, 25, 20]

Program 4: With the help of LinkedBlockingDeque.




// Java Program Demonstrate offer()
// method of Deque when Null is passed
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Deque
        Deque<Integer> DQ
            = new LinkedBlockingDeque<Integer>();
  
        if (DQ.offer(10))
            System.out.println("The Deque is not full and 10 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(15))
            System.out.println("The Deque is not full and 15 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(25))
            System.out.println("The Deque is not full and 25 is inserted");
        else
            System.out.println("The Deque is full");
  
        if (DQ.offer(20))
            System.out.println("The Deque is not full and 20 is inserted");
        else
            System.out.println("The Deque is full");
  
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}
Output:
The Deque is not full and 10 is inserted
The Deque is not full and 15 is inserted
The Deque is not full and 25 is inserted
The Deque is not full and 20 is inserted
Deque: [10, 15, 25, 20]

Below programs illustrate exceptions thrown by this method:

Program 5: To show NullPointerException.




// Java Program Demonstrate offer()
// method of Queue when Null is passed
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
  
public class GFG {
    public static void main(String[] args)
        throws NullPointerException
    {
  
        // create object of Queue
        Deque<Integer> DQ
            = new LinkedBlockingDeque<Integer>();
  
        // Add numbers to end of Deque
        DQ.offer(7855642);
        DQ.offer(35658786);
        DQ.offer(5278367);
  
        // when null is inserted
        DQ.offer(null);
  
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException
    at java.util.concurrent.LinkedBlockingDeque.offerLast(LinkedBlockingDeque.java:357)
    at java.util.concurrent.LinkedBlockingDeque.offer(LinkedBlockingDeque.java:641)
    at GFG.main(GFG.java:21)

Note: The other two exceptions are internal and are caused depending on the compiler hence it cannot be shown in the code.

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#offer-E-




My Personal Notes arrow_drop_up
Recommended Articles
Page :