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.
import java.util.concurrent.*;
public class GFG {
public static void main(String[] args)
{
ConcurrentLinkedDeque<String> CLD
= new ConcurrentLinkedDeque<String>();
CLD.push( "Welcome" );
CLD.push( "To" );
CLD.push( "Geeks" );
CLD.push( "For" );
CLD.push( "Geeks" );
System.out.println( "ConcurrentLinkedDeque: "
+ CLD);
}
}
|
Output:
ConcurrentLinkedDeque: [Geeks, For, Geeks, To, Welcome]
Program 2: To show NullPointerException.
import java.util.concurrent.*;
public class GFG {
public static void main(String[] args)
{
ConcurrentLinkedDeque<String> CLD
= new ConcurrentLinkedDeque<String>();
System.out.println( "ConcurrentLinkedDeque: "
+ CLD);
try {
System.out.println( "Trying to add "
+ "null in ConcurrentLinkedDeque" );
CLD.push( null );
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
ConcurrentLinkedDeque: []
Trying to add null in ConcurrentLinkedDeque
java.lang.NullPointerException
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
26 Dec, 2018
Like Article
Save Article