Deque addFirst() method in Java with Examples
Last Updated :
21 Sep, 2018
The addFirst(E e) method of Deque Interface inserts the element passed in the parameter to the front of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion.
Syntax:
void addFirst(E e)
Parameters: This method accepts a mandatory parameter e which is the element to be inserted to the front of the Deque.
Returns: This method returns true on successful insertion.
Exceptions: The function throws four exceptions which are described as below:
- ClassCastException: when the class of the element to be entered prevents it from being added to this container.
- IllegalStateException: when the capacity of the container is full and insertion is done.
- IllegalArgumentException: when some property of the element prevents it to be added to the Deque.
- NullPointerException: when the element to be inserted is passed as null and the Deque’s interface does not allow null elements.
Below programs illustrate addFirst() method of Deque:
Program 1: With the help of LinkedList.
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Deque<Integer> DQ
= new LinkedList<Integer>();
DQ.addFirst( 7855642 );
DQ.addFirst( 35658786 );
DQ.addFirst( 5278367 );
DQ.addFirst( 74381793 );
System.out.println( "Deque: " + DQ);
}
}
|
Output:
Deque: [74381793, 5278367, 35658786, 7855642]
Program 2: With the help of ArrayDeque.
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Deque<Integer> DQ
= new ArrayDeque<Integer>();
DQ.addFirst( 7855642 );
DQ.addFirst( 35658786 );
DQ.addFirst( 5278367 );
DQ.addFirst( 74381793 );
System.out.println( "Deque: " + DQ);
}
}
|
Output:
Deque: [74381793, 5278367, 35658786, 7855642]
Program 3: With the help of ConcurrentLinkedDeque.
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Deque<Integer> DQ
= new ConcurrentLinkedDeque<Integer>();
DQ.addFirst( 7855642 );
DQ.addFirst( 35658786 );
DQ.addFirst( 5278367 );
DQ.addFirst( 74381793 );
System.out.println( "Deque: " + DQ);
}
}
|
Output:
Deque: [74381793, 5278367, 35658786, 7855642]
Program 4: With the help of LinkedBlockingDeque.
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Deque<Integer> DQ
= new LinkedBlockingDeque<Integer>();
DQ.addFirst( 7855642 );
DQ.addFirst( 35658786 );
DQ.addFirst( 5278367 );
DQ.addFirst( 74381793 );
System.out.println( "Deque: " + DQ);
}
}
|
Output:
Deque: [74381793, 5278367, 35658786, 7855642]
Below programs illustrate exceptions thrown by addFirst() method:
Program 5: To show NullPointerException.
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Deque<Integer> DQ
= new LinkedBlockingDeque<Integer>();
DQ.addFirst( 7855642 );
DQ.addFirst( 35658786 );
DQ.addFirst( 5278367 );
DQ.addFirst( null );
System.out.println( "DeQue: " + DQ);
}
}
|
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:20)
Program 6: To show IllegalStateException.
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Deque<Integer> DQ
= new LinkedBlockingDeque<Integer>( 3 );
DQ.addFirst( 7855642 );
DQ.addFirst( 35658786 );
DQ.addFirst( 5278367 );
DQ.addFirst( 10 );
System.out.println( "Deque: " + DQ);
}
}
|
Output:
Exception in thread "main" java.lang.IllegalStateException: Deque full
at java.util.concurrent.LinkedBlockingDeque.addFirst(LinkedBlockingDeque.java:326)
at GFG.main(GFG.java:21)
Note: The other two exceptions are internal and are caused depending on the compiler hence cannot be shown in the compiler.
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#addFirst-E-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...