Wednesday, July 3, 2024
HomeLanguagesJavaBlockingDeque offerLast() method in Java with examples

BlockingDeque offerLast() method in Java with examples

The offerLast(E e) method of BlockingDeque 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 BlockingDeque.

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

Note: The offerLast() method of BlockingDeque has been inherited from the LinkedBlockingDeque class in Java.

Below programs illustrate offerLast() method of BlockingDeque:

Program 1:




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


Output:

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

Program 2:




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


Output:

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

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments