Skip to content
Related Articles

Related Articles

ConcurrentLinkedDeque push() method in Java with Examples

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 26 Dec, 2018

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.




// 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);
    }
}

Output:

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

Program 2: To show NullPointerException.




// 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);
        }
    }
}

Output:

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!