ConcurrentLinkedDeque push() method in Java with Examples

The push() method of ConcurrentLinkedDeque class is an in-built function in Java which pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

Syntax:

public void push(E e)

Here, E is the type of element maintained 
by this collection class.

Parameter: This method accepts only a single parameter element which is to be added at the head of the ConcurentLinkedDeque.

Return Value:The function has no return value.

Exception:The method will throw the following exceptions.

  • IllegalStateException: if the element cannot be added at this time due to capacity restrictions.
  • ClassCastException: if the class of the specified element prevents it from being added to this deque.
  • NullPointerException: if the specified element is null and this deque does not permit null elements.
  • IllegalArgumentException: if some property of the specified element prevents it from being added to this deque.

Below programs illustrate the ConcurrentLinkedDeque.push() method:

Program 1: This program involves a ConcurrentLinkedDeque of Character type.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate push()
// method of ConcurrentLinkedDeque
  
import java.util.concurrent.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Creating an empty ConcurrentLinkedDeque
        ConcurrentLinkedDeque<String> CLD
            = new ConcurrentLinkedDeque<String>();
  
        // Use push() method to add elements
        CLD.push("Welcome");
        CLD.push("To");
        CLD.push("Geeks");
        CLD.push("For");
        CLD.push("Geeks");
  
        // Displaying the ConcurrentLinkedDeque
        System.out.println("ConcurrentLinkedDeque: "
                           + CLD);
    }
}

chevron_right


Output:

ConcurrentLinkedDeque: [Geeks, For, Geeks, To, Welcome]

Program 2: To show NullPointerException.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate push()
// method of ConcurrentLinkedDeque
  
import java.util.concurrent.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Creating an empty ConcurrentLinkedDeque
        ConcurrentLinkedDeque<String> CLD
            = new ConcurrentLinkedDeque<String>();
  
        // Displaying the ConcurrentLinkedDeque
        System.out.println("ConcurrentLinkedDeque: "
                           + CLD);
  
        try {
  
            System.out.println("Trying to add "
                               + "null in ConcurrentLinkedDeque");
  
            // Use push() method to null element
            CLD.push(null);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

ConcurrentLinkedDeque: []
Trying to add null in ConcurrentLinkedDeque
java.lang.NullPointerException


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.