The addFirst(E e) method of LinkedBlockingDeque inserts the element passed in the parameter to the front of the Deque if there is space. If the LinkedBlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateException.
Syntax:
public void addFirst(E e)
Parameters: This method accepts a mandatory parameter e which is the element to be inserted in the front of the LinkedBlockingDeque.
Returns: This method does not returns anything.
Exception:
- IllegalStateException: if the element cannot be added at this time due to capacity restrictions
- NullPointerException: if the specified element is null
Below programs illustrate addFirst() method of PriorityBlockingQueue:
Program 1:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
LinkedBlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>();
LBD.addFirst( 7855642 );
LBD.addFirst( 35658786 );
LBD.addFirst( 5278367 );
LBD.addFirst( 74381793 );
System.out.println( "Linked Blocking Deque: " + LBD);
}
}
|
Output:
Linked Blocking Deque: [74381793, 5278367, 35658786, 7855642]
Program 2:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
LinkedBlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>( 3 );
LBD.addFirst( 7855642 );
LBD.addFirst( 35658786 );
LBD.addFirst( 5278367 );
LBD.addFirst( 74381793 );
System.out.println( "Linked Blocking Deque: " + LBD);
}
}
|
Output:
Exception in thread "main" java.lang.IllegalStateException: Deque full
at java.util.concurrent.LinkedBlockingDeque.addFirst(LinkedBlockingDeque.java:326)
at GFG.main(GFG.java:23)
Program 3:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
LinkedBlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>();
LBD.addFirst( 7855642 );
LBD.addFirst( 35658786 );
LBD.addFirst( 5278367 );
LBD.addFirst( null );
System.out.println( "Linked Blocking Deque: " + LBD);
}
}
|
Output:
Exception in thread "main" java.lang.NullPointerException
at java.util.concurrent.LinkedBlockingDeque.offerFirst(LinkedBlockingDeque.java:342)
at java.util.concurrent.LinkedBlockingDeque.addFirst(LinkedBlockingDeque.java:325)
at GFG.main(GFG.java:22)
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#add(E)
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
14 Sep, 2018
Like Article
Save Article