ConcurrentLinkedDeque method addFirst() in Java
A ConcurrentLikedDeque method addFirst() inserts the specified element at the front of this deque. As the deque is unbounded, this method will never throw IllegalStateException.
Syntax
 1 public void addFirst(E e)
e : It is an element to be inserted at front in this deque.
ConcurrentLinkedDeque method addFirst()
 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         // add first element
 15         cdeque.addFirst(1000);
 16         System.out.println("A ConcurrentLikedDeque elements : " + cdeque);
 17     }
 18 }
In the above example, a ConcurrentLinkedDeque created and added elements using addAll() method by passing a collection. A addFirst() method called by passing an integer value that inserts specified value at the front of this deque and print updated deque that includes newly added element at first position.
Output
 1 A ConcurrentLikedDeque elements : [120, 135, 140, 145]
 2 A ConcurrentLikedDeque elements : [1000, 120, 135, 140, 145]

ConcurrentLinkedDeque method addFirst() with String

ConcurrentLinkedDeque method addFirst() with String
 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         // add first element
 15         cdeque.addFirst("THANKS");
 16         System.out.println("A ConcurrentLikedDeque elements : " + cdeque);
 17     }
 18 }
In the above example, a ConcurrentLinkedDeque created and added elements using addAll() method by passing a collection. A addFirst() method called by passing a String that inserts specified string at the front of this deque and print updated deque that includes newly added string at first position.
Output
 1 A ConcurrentLikedDeque elements : [Welcome, to, IOGyan]
 2 A ConcurrentLikedDeque elements : [THANKS, Welcome, to, IOGyan]

ConcurrentLinkedDeque method addFirst() with custom object

ConcurrentLinkedDeque method addFirst() with custom object
 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         // add first element
 19         cdeque.addFirst(emp3);
 20         System.out.println("A ConcurrentLikedDeque elements : " + cdeque);
 21     }
 22 }
In the above example, a ConcurrentLinkedDeque created and added elements using addAll() method by passing a collection. A addFirst() method called by passing an employee object that inserts specified object at the front of this deque and print updated deque that includes newly added employee object at first position. It internally uses toString() method of Employee class to print Employee object.
Output
 1 A ConcurrentLikedDeque elements : [Emp2 Tech 31000, Emp1 Tech 12000]
 2 A ConcurrentLikedDeque elements : [Emp3 Admin 21000, Emp2 Tech 31000, Emp1 Tech 12000]

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