Skip to content
Related Articles

Related Articles

LinkedBlockingDeque offerLast() method in Java
  • Last Updated : 14 Sep, 2018

The offerLast(E e) method of LinkedBlockingDeque inserts the element passed in the parameter at the end of the Deque container. If the container’s capacity has exceeded, then it does not returns an exception as in case of add() and addLast() function.

Syntax:

public boolean offerLast(E e)

Parameters: This method accepts a mandatory parameter e which is the element to be inserted at the end of the LinkedBlockingDeque.

Returns: This method returns true if the element has been inserted, else it returns false.

Below programs illustrate offerLast() method of LinkedBlockingDeque:



Program 1:




// Java Program Demonstrate offerLast()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque<Integer> LBD
            = new LinkedBlockingDeque<Integer>(4);
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.offerLast(7855642);
        LBD.offerLast(35658786);
        LBD.offerLast(5278367);
        LBD.offerLast(74381793);
  
        // Cannot be inserted
        LBD.offerLast(10);
  
        // cannot be inserted hence returns false
        if (!LBD.offerLast(10))
            System.out.println("The element 10 cannot be inserted"+
                                            " as capacity is full");
  
        // before removing print queue
        System.out.println("Linked Blocking Deque: " + LBD);
    }
}


Output:

The element 10 cannot be inserted as capacity is full
Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]

Program 2:




// Java Program Demonstrate offerLast()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque<String> LBD
            = new LinkedBlockingDeque<String>(4);
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.offerLast("abc");
        LBD.offerLast("gopu");
        LBD.offerLast("geeks");
        LBD.offerLast("richik");
  
        // Cannot be inserted
        LBD.offerLast("hii");
  
        // cannot be inserted hence returns false
        if (!LBD.offerLast("hii"))
            System.out.println("The element 'hii' cannot be"+
                             " inserted as capacity is full");
  
        // before removing print queue
        System.out.println("Linked Blocking Deque: " + LBD);
    }
}


Output:

The element 'hii' cannot be inserted as capacity is full
Linked Blocking Deque: [abc, gopu, geeks, richik]

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#offerLast(E)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :