Open In App

Deque add() method in Java

The add(E e) method of Deque Interface inserts the element passed in the parameter to the end of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion.

Syntax:  



boolean add(E e)

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

Returns: This method returns true on successful insertion. 



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

Below programs illustrate add() method of Deque:

Program 1: With the help of LinkedList.  




// Java Program Demonstrate add()
// method of Deque
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of De1ue
        Deque<Integer> DQ
            = new LinkedList<Integer>();
 
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}

Output: 
Deque: [7855642, 35658786, 5278367, 74381793]




 

Program 2: With the help of ArrayDeque




// Java Program Demonstrate add()
// method of Deque
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of De1ue
        Deque<Integer> DQ
            = new ArrayDeque<Integer>();
 
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}

Output: 
Deque: [7855642, 35658786, 5278367, 74381793]




 

Program 3: With the help of ConcurrentLinkedDeque




// Java Program Demonstrate add()
// method of Deque
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
 
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
 
        // create object of De1ue
        Deque<Integer> DQ
            = new ConcurrentLinkedDeque<Integer>();
 
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
 
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}

Output: 
Deque: [7855642, 35658786, 5278367, 74381793]




 

Program 4: With the help of LinkedBlockingDeque




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

Output: 
Deque: [7855642, 35658786, 5278367, 74381793]




 

Below programs illustrate exceptions thrown by add() method:

Program 5: To show NullPointerException




/// Java Program Demonstrate add()
// 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>();
 
        // Add numbers to end of DeQue
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        // when null is inserted
        DQ.add(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.addLast(LinkedBlockingDeque.java:334)
    at java.util.concurrent.LinkedBlockingDeque.add(LinkedBlockingDeque.java:633)
    at GFG.main(GFG.java:20)

Program 6: To show IllegalStateException.




// Java Program Demonstrate add()
// method of Deque when capacity is full
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>(3);
 
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
 
        // when capacity is full
        DQ.add(10);
 
        // before removing print Deque
        System.out.println("Deque: " + DQ);
    }
}

Output: 

Exception in thread "main" java.lang.IllegalStateException: Deque full
    at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:335)
    at java.util.concurrent.LinkedBlockingDeque.add(LinkedBlockingDeque.java:633)
    at GFG.main(GFG.java:21)

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

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


Article Tags :