ConcurrentLinkedDeque in Java with Examples - GeeksforGeeks

16 Sep, 2020
The ConcurrentLinkedDeque class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. It belongs to java.util.concurrent package. It is used to implement Deque with the help of LinkedList concurrently.
Features of ConcurrentLinkedDeque
Iterators and spliterators are weakly consistent.
Concurrent insertion, removal, and access operations execute safely across multiple threads.
It does not permit null elements.
size() method is not implemented in constant time. Because of the asynchronous nature of these deques, determining the current number of elements requires a traversal of the elements.
Class Hierarchy: 
Declaration:  
public abstract class ConcurrentLinkedDeque<E>
   extends AbstractCollection<E>
      implements Deque<E>, Serializable

Here, E is the type of elements maintained by this collection.

It implements Serializable, Iterable<E>, Collection<E>, Deque<E>, Queue<E> interfaces.
1. ConcurrentLinkedDeque(): This constructor is used to construct an empty deque.
ConcurrentLinkedDeque<E> cld = new ConcurrentLinkedDeque<E>();
2. ConcurrentLinkedDeque(Collection<E> c): This constructor is used to construct a deque with the elements of the Collection passed as the parameter.
ConcurrentLinkedDeque<E> cld = new ConcurrentLinkedDeque<E>(Collection<E> c);
Below is the sample program to illustrate ConcurrentLinkedDeque in Java:  
Java
// Java Program to demonstrate ConcurrentLinkedDeque  import java.util.concurrent.*;  class ConcurrentLinkedDequeDemo {      public static void main(String[] args)    {        // Create a ConcurrentLinkedDeque        // using ConcurrentLinkedDeque()         // constructor        ConcurrentLinkedDeque<Integer>            cld = new ConcurrentLinkedDeque<Integer>();                    // add element to the front          // using addFirst() method        cld.addFirst(12);        cld.addFirst(70);        cld.addFirst(1009);        cld.addFirst(475);          // Displaying the existing ConcurrentLinkedDeque        System.out.println("ConcurrentLinkedDeque: "                           + cld);          // Create a ConcurrentLinkedDeque        // using ConcurrentLinkedDeque(Collection c)         // constructor        ConcurrentLinkedDeque<Integer>            cld1 = new ConcurrentLinkedDeque<Integer>(cld);          // Displaying the existing ConcurrentLinkedDeque        System.out.println("ConcurrentLinkedDeque1: "                           + cld1);    }}
ConcurrentLinkedDeque: [475, 1009, 70, 12]
ConcurrentLinkedDeque1: [475, 1009, 70, 12]
 
Example:   
Java
// Java code to illustrate// methods of ConcurrentLinkedDeque  import java.util.concurrent.*;  class ConcurrentLinkedDequeDemo {      public static void main(String[] args)    {          // Create a ConcurrentLinkedDeque        // using ConcurrentLinkedDeque() constructor        ConcurrentLinkedDeque<Integer>            cld = new ConcurrentLinkedDeque<Integer>();                    // add element to the front          // using addFirst() method        cld.addFirst(12);        cld.addFirst(70);        cld.addFirst(1009);        cld.addFirst(475);          // Displaying the existing ConcurrentLinkedDeque        System.out.println("ConcurrentLinkedDeque: "                           + cld);          // Displaying the Last element        // using getLast() method        System.out.println("The Last element is: "                           + cld.getLast());          // Displaying the first element        // using peekFirst() method        System.out.println("First Element is: "                           + cld.peekFirst());          // Remove the Last element        // using removeLast() method        cld.removeLast();          // Displaying the existing ConcurrentLinkedDeque        System.out.println("ConcurrentLinkedDeque: "                           + cld);    }}
ConcurrentLinkedDeque: [475, 1009, 70, 12]
The Last element is: 12
First Element is: 475
ConcurrentLinkedDeque: [475, 1009, 70]
 
1. Adding Elements
To add an element or Collection of elements, ConcurrentLinkedDeque provides methods like add(E e), addAll(Collection<? extends E> c), addFirst(E e), addLast(E e) methods. The example below explains these methods.
Java
// Java Program Demonstrate adding// elements to the ConcurrentLinkedDeque  import java.util.concurrent.*;  class AddingElements {      public static void main(String[] args)    {        // create instance using ConcurrentLinkedDeque        ConcurrentLinkedDeque<Integer> cld1            = new ConcurrentLinkedDeque<Integer>();          // Add element to the tail using        // add or addLast methods        cld1.add(12);        cld1.add(110);          // Add element to the head        // using addFirst method        cld1.addFirst(55);          // Displaying the existing ConcurrentLinkedDeque        System.out.println("Initial Elements in "                           + "the LinkedDeque cld : "                           + cld1);          // create instance using ConcurrentLinkedDeque        ConcurrentLinkedDeque<Integer> cld2            = new ConcurrentLinkedDeque<Integer>();          // Add elements of cld1 to the        // cld2 using addAll method        cld2.addAll(cld1);          // Displaying the modified ConcurrentLinkedDeque        System.out.println("Initial Elements in "                           + "the LinkedDeque cld2: "                           + cld2);    }}
 Output:
Initial Elements in the LinkedDeque cld : [55, 12, 110]
Initial Elements in the LinkedDeque cld2: [55, 12, 110]
2. Remove Elements
To remove an element, ConcurrentLinkedDeque provides methods like remove(), remove(Object o), removeFirst(), removeLast() etc. These methods are explained in the below example.
Java
// Java Program to demonstrate removing// elements of ConcurrentLinkedDeque  import java.util.concurrent.*;  class RemovingElements {    public static void main(String[] args)    {          // Create a ConcurrentLinkedDeque        // using ConcurrentLinkedDeque() constructor        ConcurrentLinkedDeque<Integer> cld            = new ConcurrentLinkedDeque<Integer>();          // Add elements using add() method        cld.add(40);        cld.add(50);        cld.add(60);        cld.add(70);        cld.add(80);          // Displaying the existing LinkedDeque        System.out.println(            "Existing ConcurrentLinkedDeque: " + cld);          // remove method removes the first        // element of ConcurrentLinkedDeque        // using remove() method        System.out.println("Element removed: "                           + cld.remove());          // Remove 60 using remove(Object)        System.out.println("60 removed: " + cld.remove(60));          // Displaying the existing ConcurrentLinkedDeque        System.out.println(            "Modified ConcurrentLinkedDeque: " + cld);          // Remove the first element        cld.removeFirst();          // Remove the Last element        cld.removeLast();          // Displaying the existing ConcurrentLinkedDeque        System.out.println(            "Modified ConcurrentLinkedDeque: " + cld);    }}
Existing ConcurrentLinkedDeque: [40, 50, 60, 70, 80]
Element removed: 40
60 removed: true
Modified ConcurrentLinkedDeque: [50, 70, 80]
Modified ConcurrentLinkedDeque: [70]
3. Iterating Elements 
We can iterate the ConcurrentLinkedDeque using iterator() or descendingIterator() methods. The below code explains both these methods.
Java
// Java code to illustrate iterating// elements of ConcurrentLinkedDeque  import java.util.concurrent.*;import java.util.*;  public class IteratingConcurrentLinkedDeque {      public static void main(String args[])    {        // Creating an empty ConcurrentLinkedDeque        ConcurrentLinkedDeque<String> deque            = new ConcurrentLinkedDeque<String>();          // Use add() method to add elements        // into the ConcurrentLinkedDeque        deque.add("Welcome");        deque.add("To");        deque.add("Geeks");        deque.add("4");        deque.add("Geeks");          // Displaying the ConcurrentLinkedDeque        System.out.println("ConcurrentLinkedDeque: "                           + deque);          // Creating an iterator        Iterator fitr = deque.iterator();          // Displaying the values        // after iterating through the ConcurrentLinkedDeque        System.out.println("The iterator values are: ");        while (fitr.hasNext()) {            System.out.println(fitr.next());        }          // Creating a desc_iterator        Iterator ditr = deque.descendingIterator();          // Displaying the values after iterating        // through the ConcurrentLinkedDeque        // in reverse order        System.out.println("The iterator values are: ");        while (ditr.hasNext()) {            System.out.println(ditr.next());        }    }}
ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]
The iterator values are: 
Welcome
To
Geeks
4
Geeks
The iterator values are: 
Geeks
4
Geeks
To
Welcome
4. Accessing Elements
To access the elements of ConcurrentLinkedDeque, it provides methods like getFirst(), getLast(), element() methods. The below example explains these methods.
Java
// Java Program to Demonstrate accessing// elements of ConcurrentLinkedDeque  import java.util.concurrent.*;import java.util.*;  class Accessing {    public static void main(String[] args)    {          // Creating an empty ConcurrentLinkedDeque        ConcurrentLinkedDeque<String> cld            = new ConcurrentLinkedDeque<String>();          // Add elements into the ConcurrentLinkedDeque        cld.add("Welcome");        cld.add("To");        cld.add("Geeks");        cld.add("4");        cld.add("Geeks");          // Displaying the ConcurrentLinkedDeque        System.out.println("Elements in the ConcurrentLinkedDeque: " + cld);          // Displaying the first element        System.out.println("The first element is: "                           + cld.getFirst());          // Displaying the Last element        System.out.println("The Last element is: "                           + cld.getLast());          // Displaying the head of ConcurrentLinkedDeque        System.out.println("The Head of ConcurrentLinkedDeque is: "                           + cld.element());    }}
Output: 
Elements in the ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]
The first element is: Welcome
The Last element is: Geeks
The Head of ConcurrentLinkedDeque is: Welcome
Here, E is the type of element.
METHOD
DESCRIPTION
METHOD
DESCRIPTION
METHOD
DESCRIPTION
METHOD
DESCRIPTION
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/ConcurrentLinkedDeque.html
Ganeshchowdharysadanala
Java - util package
Java-Collections
Java-ConcurrentLinkedDeque
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Initialize an ArrayList in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Multidimensional Arrays in Java
Stream In Java
Singleton Class in Java
Set in Java