BlockingDeque offerFirst() method in Java with Examples
The offerFirst(E e) method of BlockingDeque 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 BlockingDeque.
Returns: This method returns true if the element has been inserted, else it returns false.
Note: The offerFirst() method of BlockingDeque has been inherited from the LinkedBlockingDeque class in Java.
Below programs illustrate offerFirst() method of LinkedBlockingDeque:
Program 1:
Java
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>( 4 );
BD.offerFirst( 7855642 );
BD.offerFirst( 35658786 );
BD.offerFirst( 5278367 );
BD.offerFirst( 74381793 );
BD.offerFirst( 10 );
if (!BD.offerFirst( 10 ))
System.out.println( "The element 10 cannot be inserted"
+ " as capacity is full" );
System.out.println( "Blocking Deque: " + BD);
}
}
|
Output:
The element 10 cannot be inserted as capacity is full
Blocking Deque: [74381793, 5278367, 35658786, 7855642]
Program 2:
Java
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
BlockingDeque<String> BD
= new LinkedBlockingDeque<String>( 4 );
BD.offerFirst( "abc" );
BD.offerFirst( "gopu" );
BD.offerFirst( "geeks" );
BD.offerFirst( "richik" );
BD.offerFirst( "hii" );
if (!BD.offerFirst( "hii" ))
System.out.println( "The element 'hii' cannot be"
+ " inserted as capacity is full" );
System.out.println( "Blocking Deque: " + BD);
}
}
|
Output:
The element 'hii' cannot be inserted as capacity is full
Blocking Deque: [richik, geeks, gopu, abc]
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#offerFirst(E)
Last Updated :
25 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...