LinkedBlockingDeque method offerLast() in Java
A LinkedBlockingDeque method offerLast() inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to the addLast() method, which can fail to insert an element only by throwing an exception. It returns boolean value true, if this deque changed, otherwise false.
Syntax
 1 public boolean offerLast(E e)
e : It is an element to be inserted at the last position in this deque.
return : It returns boolean value true, if this deque changed, otherwise false.
LinkedBlockingDeque method offerLast()
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.LinkedBlockingDeque;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) {
 9 
 10         LinkedBlockingDeque<Integer> deque = new LinkedBlockingDeque(5);
 11         deque.addAll(Arrays.asList(120, 135, 140, 145));
 12         System.out.println("A LinkedBlockingDeque elements : " + deque);
 13 
 14         // insert element at last position in this deque
 15         boolean res = deque.offerLast(1000);
 16         System.out.println("Does this deque changed : " + res);
 17         System.out.println("A LinkedBlockingDeque elements : " + deque);
 18     }
 19 }
In the above example, a LinkedBlockingDeque of type Integer created and added elements using addAll() method by passing a collection. A offerLast() method called by passing a value that inserts specified value at last position in this deque and returns boolean value true, as this deque changed.
Output
 1 A LinkedBlockingDeque elements : [120, 135, 140, 145]
 2 Does this deque changed : true
 3 A LinkedBlockingDeque elements : [120, 135, 140, 145, 1000]

LinkedBlockingDeque method offerLast() with String

LinkedBlockingDeque method offerLast() with String
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.LinkedBlockingDeque;
 5 
 6 public class StringExp {
 7 
 8     public static void main(String[] args) {
 9 
 10         LinkedBlockingDeque<String> deque = new LinkedBlockingDeque();
 11 
 12         deque.addAll(Arrays.asList("Welcome", "to", "IOGyan"));
 13         System.out.println("A LinkedBlockingDeque elements : " + deque);
 14 
 15         // insert element at last position in this deque
 16         boolean res = deque.offerLast("THANKS");
 17         System.out.println("Does this deque changed : " + res);
 18         System.out.println("A LinkedBlockingDeque elements : " + deque);
 19     }
 20 }
In the above example, a LinkedBlockingDeque of type String created and added elements using addAll() method by passing a collection. A offerLast() method called by passing a string that inserts specified string at last position in this deque and returns boolean value true, as this deque changed.
Output
 1 A LinkedBlockingDeque elements : [Welcome, to, IOGyan]
 2 Does this deque changed : true
 3 A LinkedBlockingDeque elements : [Welcome, to, IOGyan, THANKS]

LinkedBlockingDeque method offerLast() with custom object

LinkedBlockingDeque method offerLast() with custom object
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.LinkedBlockingDeque;
 5 
 6 public class Example {
 7 
 8     public static void main(String[] args) {
 9 
 10         Employee emp1 = new Employee(100, "Emp1", "Tech",  12000);
 11         Employee emp2 = new Employee(200, "Emp2", "Tech", 31000);
 12         Employee emp3 = new Employee(300,"Emp3", "Admin", 21000);
 13 
 14         LinkedBlockingDeque<Employee> deque = new LinkedBlockingDeque();
 15         deque.addAll(Arrays.asList(emp2, emp1));
 16         System.out.println("A LinkedBlockingDeque elements : " + deque);
 17 
 18         // insert element at last position in this deque
 19         boolean res = deque.offerLast(emp3);
 20         System.out.println("Does this deque changed : " + res);
 21         System.out.println("A LinkedBlockingDeque elements : " + deque);
 22     }
 23 }
In the above example, a LinkedBlockingDeque of type Employee created and added elements using addAll() method by passing a collection. A offerLast() method called by passing Employee object that inserts specified object at last position in this deque and returns boolean value true, as this deque changed.
Output
 1 A LinkedBlockingDeque elements : [Emp2 Tech 31000, Emp1 Tech 12000]
 2 Does this deque changed : true
 3 A LinkedBlockingDeque elements : [Emp2 Tech 31000, Emp1 Tech 12000, Emp3 Admin 21000]

LinkedBlockingDeque method offerLast() with time

A LinkedBlockingDeque method offerLast() inserts the specified element at the last position in this deque, waiting up to the specified wait time if necessary for space to become available. It returns boolean value true, if this deque changed, otherwise false.
Syntax
 1 public boolean offerLast(E e, long timeout, TimeUnit unit) throws InterruptedException
e : It is an element to be inserted at last position in this deque.
timeout : It specifies how long to wait before giving up, in units of unit
unit : It specifies a TimeUnit determining how to interpret the timeout parameter
return : It returns boolean value true, if this deque changed, otherwise false.

Exceptions:

1. NullPointerException : if the specified element is null2. InterruptedException : if interrupted while waiting
LinkedBlockingDeque method offerLast() with time
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.LinkedBlockingDeque;
 5 import java.util.concurrent.TimeUnit;
 6 
 7 public class Main {
 8 
 9     public static void main(String[] args) throws InterruptedException {
 10 
 11         LinkedBlockingDeque<Integer> deque = new LinkedBlockingDeque(5);
 12         deque.addAll(Arrays.asList(120, 135, 140, 145));
 13         System.out.println("A LinkedBlockingDeque elements : " + deque);
 14 
 15         // insert element at last position in this deque
 16         boolean res = deque.offerLast(1000, 5, TimeUnit.SECONDS);
 17         System.out.println("Does this deque changed : " + res);
 18         System.out.println("A LinkedBlockingDeque elements : " + deque);
 19     }
 20 }
In the above example, a LinkedBlockingDeque of type Integer created and added elements using addAll() method by passing a collection. A offerLast() method called by passing a value, timeout and unit that waits for 5 seconds if this deque is full and inserts the specified value at last position in this deque and returns boolean value true, as this deque changed. If this deque does not have capacity to insert specified element, it waits for specified interval and throw exception.
Output
 1 A LinkedBlockingDeque elements : [120, 135, 140, 145]
 2 Does this deque changed : true
 3 A LinkedBlockingDeque elements : [120, 135, 140, 145, 1000]

LinkedBlockingDeque method offerLast() with String and time

LinkedBlockingDeque method offerLast() with String and time
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.LinkedBlockingDeque;
 5 import java.util.concurrent.TimeUnit;
 6 
 7 public class StringExp {
 8 
 9     public static void main(String[] args) throws InterruptedException {
 10 
 11         LinkedBlockingDeque<String> deque = new LinkedBlockingDeque();
 12 
 13         deque.addAll(Arrays.asList("Welcome", "to", "IOGyan"));
 14         System.out.println("A LinkedBlockingDeque elements : " + deque);
 15 
 16         // insert element at last position in this deque
 17         boolean res = deque.offerLast("THANKS", 5, TimeUnit.SECONDS);
 18         System.out.println("Does this deque changed : " + res);
 19         System.out.println("A LinkedBlockingDeque elements : " + deque);
 20     }
 21 }
In the above example, a LinkedBlockingDeque of type String created and added elements using addAll() method by passing a collection. A offerLast() method called by passing a string, timeout and unit that waits for 5 seconds if this deque is full and inserts the specified value at last position in this deque and returns boolean value true, as this deque changed. If this deque does not have capacity to insert specified element, it waits for specified interval and throw exception.
Output
 1 A LinkedBlockingDeque elements : [Welcome, to, IOGyan]
 2 Does this deque changed : true
 3 A LinkedBlockingDeque elements : [Welcome, to, IOGyan, THANKS]

Employee class

Employee class
 1 package com.java;
 2 
 3 public class Employee {
 4 
 5     private final int id;
 6     private final String name;
 7     private final String dept;
 8     private final Long salary;
 9 
 10     public Employee(int id, String name, String dept, long salary) {
 11         this.id = id;
 12         this.name = name;
 13         this.dept = dept;
 14         this.salary = salary;
 15     }
 16     public int getId() {
 17         return this.id;
 18     }
 19 
 20     public String getDept() {
 21         return this.dept;
 22     }
 23 
 24     public String getName() {
 25         return this.name;
 26     }
 27 
 28     public Long getSalary() {
 29         return this.salary;
 30     }
 31 
 32     @Override
 33     public int hashCode() {
 34         return this.getId() + this.getName().hashCode();
 35     }
 36 
 37     @Override
 38     public boolean equals(Object obj) {
 39         return this.id == ((Employee)obj).id;
 40     }
 41 
 42     public String toString() {
 43         return this.name + " " + this.dept + " " + this.salary;
 44     }
 45 }
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us