ConcurrentLinkedDeque method add() in Java
A ConcurrentLinkedDeque method add() inserts the specified element at the tail of this deque. As the deque is unbounded, this method will never throw IllegalStateException or return false. It returns boolean value true, if this deque changed.
Syntax
 1 public boolean add(E e)
e : It is an element to be inserted at the end of this deque.
return : It returns boolean value true, if this deque changed.

Exception:

1. NullPointerException : if the specified element is null
ConcurrentLinkedDeque method add()
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.ConcurrentLinkedDeque;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) {
 9 
 10         ConcurrentLinkedDeque<Integer> cdeque = new ConcurrentLinkedDeque();
 11         cdeque.addAll(Arrays.asList(120, 135, 140, 145));
 12         System.out.println("A ConcurrentLikedDeque elements : " + cdeque);
 13 
 14         // insert element at the end of this deque
 15         boolean res = cdeque.add(1000);
 16         System.out.println("Does this deque changed : " + res);
 17         System.out.println("A ConcurrentLikedDeque elements : " + cdeque);
 18     }
 19 }
In the above example, a ConcurrentLinkedDeque of type Integer created and added elements using addAll() method by passing a collection. A add() method called by passing a value that inserts specified value at the end of this deque and returns boolean value true, as this deque changed and print updated deque.
Output
 1 A ConcurrentLikedDeque elements : [120, 135, 140, 145]
 2 Does this deque changed : true
 3 A ConcurrentLikedDeque elements : [120, 135, 140, 145, 1000]

ConcurrentLinkedDeque method add()

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

ConcurrentLinkedDeque method add()

ConcurrentLinkedDeque method add()
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.ConcurrentLinkedDeque;
 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         ConcurrentLinkedDeque<Employee> cdeque = new ConcurrentLinkedDeque();
 15         cdeque.addAll(Arrays.asList(emp2, emp1));
 16         System.out.println("A ConcurrentLikedDeque elements : " + cdeque);
 17 
 18         // insert element at the end of this deque
 19         boolean res = cdeque.add(emp3);
 20         System.out.println("Does this deque changed : " + res);
 21         System.out.println("A ConcurrentLikedDeque elements : " + cdeque);
 22     }
 23 }
In the above example, a ConcurrentLinkedDeque of type Employee created and added elements using addAll() method by passing a collection. A add() method called by passing an Employee object that inserts specified object at the end of this deque and returns boolean value true, as this deque changed and print updated deque. It internally uses Employee class toString() method to print Employee object.
Output
 1 A ConcurrentLikedDeque elements : [Emp2 Tech 31000, Emp1 Tech 12000]
 2 Does this deque changed : true
 3 A ConcurrentLikedDeque elements : [Emp2 Tech 31000, Emp1 Tech 12000, Emp3 Admin 21000]

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