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.
// 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); } } |
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); } } } |
ConcurrentLinkedDeque: [] Trying to add null in ConcurrentLinkedDeque java.lang.NullPointerException
Recommended Posts:
- ConcurrentLinkedDeque contains() method in Java with Examples
- ConcurrentLinkedDeque pop() method in Java with Examples
- ConcurrentLinkedDeque addAll() method in Java with Examples
- ConcurrentLinkedDeque isEmpty() method in Java with Examples
- ConcurrentLinkedDeque offer() method in Java with Examples
- ConcurrentLinkedDeque Spliterator() method in Java with Examples
- ConcurrentLinkedDeque remove() method in Java with Examples
- BlockingDeque push() method in Java with examples
- ConcurrentLinkedDeque in Java with Examples
- ConcurrentLinkedDeque add() method in Java
- ConcurrentLinkedDeque poll() method in Java with Example
- ConcurrentLinkedDeque peek() method in Java with Example
- ConcurrentLinkedDeque pollLast() method in Java
- ConcurrentLinkedDeque equals() method in Java with Example
- ConcurrentLinkedDeque hashCode() method in Java with Example
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.



