LinkedBlockingDeque offerFirst() method in Java
Last Updated :
20 Nov, 2020
The offerFirst(E e) method of LinkedBlockingDeque inserts the element passed in the parameter at the front of the Deque container. If the container’s capacity has exceeded, then it does not returns an exception as in case of add() and addFirst() function.
Syntax:
public boolean offerFirst(E e)
Parameters: This method accepts a mandatory parameter e which is the element to be inserted at the front of the LinkedBlockingDeque.
Returns: This method returns true if the element has been inserted, else it returns false.
Below programs illustrate offerFirst() method of LinkedBlockingDeque:
Program 1:
Java
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>( 4 );
LBD.offerFirst( 7855642 );
LBD.offerFirst( 35658786 );
LBD.offerFirst( 5278367 );
LBD.offerFirst( 74381793 );
LBD.offerFirst( 10 );
if (!LBD.offerFirst( 10 ))
System.out.println( "The element 10 cannot be inserted" +
" as capacity is full" );
System.out.println( "Linked Blocking Deque: " + LBD);
}
}
|
Output:
The element 10 cannot be inserted as capacity is full
Linked Blocking Deque: [74381793, 5278367, 35658786, 7855642]
Program 2:
Java
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
LinkedBlockingDeque<String> LBD
= new LinkedBlockingDeque<String>( 4 );
LBD.offerFirst( "abc" );
LBD.offerFirst( "gopu" );
LBD.offerFirst( "geeks" );
LBD.offerFirst( "richik" );
LBD.offerFirst( "hii" );
if (!LBD.offerFirst( "hii" ))
System.out.println( "The element 'hii' cannot be"
+ " inserted as capacity is full" );
System.out.println( "Linked Blocking Deque: " + LBD);
}
}
|
Output:
The element 'hii' cannot be inserted as capacity is full
Linked Blocking Deque: [richik, geeks, gopu, abc]
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#offerFirst(E)
Share your thoughts in the comments
Please Login to comment...