Open In App
Related Articles

Java Program to Implement ConcurrentLinkedDeque API

Improve Article
Improve
Save Article
Save
Like Article
Like

ConcurrentLinkedDeque class in Java is an unbounded concurrent deque that stores its elements as linked nodes where each node contains the address of the previous as well as next nodes. It belongs to java.util.concurrent package. This class is a member of the Java Collections Framework. It also extends Object and AbstractCollection classes.

Features of ConcurrentLinkedDeque API

  • It does not allow null elements.
  • Iterators are weakly consistent.
  • Concurrent insertion, removal, and access operations execute safely across multiple threads so it is thread-safe.
  • The size method is NOT a constant-time operation

Implementing interfaces

1. Serializable 
2. Iterable<E>
3. Collection<E>
4. Deque<E>
5. Queue<E>

Parameters: E — The type of elements in the collection

Syntax:

public class ConcurrentLinkedDeque<E> 
extends AbstractCollection<E>
implements Deque<E>, Serializable

Constructors :

  1. public ConcurrentLinkedDeque(): It creates an empty deque.
  2. public ConcurrentLinkedDeque(Collection<E> c): It creates a deque that initially contains the elements of the Collection<E>.

Methods:

Method     TypeDescription
add(E e)      boolean  Inserts an element in the tail of the deque
addAll(Collection<E> c)   boolean  Inserts all the elements present in the specified Collection
addFirst(E e)   void Adds an element in the front of the deque
addLast(E e)    void Adds an element in the last of the deque
clear()  voidRemoves all the elements from the deque
contains(Object o)      boolean  Returns true if the deque contains the Object O
descendingIterator() Iterator<E> Returns an iterator over the elements in the deque in reverse order.
 element()      ERetrieves the head of the deque without removing it
getFirst()       E Retrieves the first element of the deque
getLast()      E Retrieves the last element of the deque
isEmpty()     boolean Returns true if the deque contains no elements 
iterator()   Iterator<E>  Returns an iterator over the elements in the deque 
peek()         E Retrieves the head of the deque without removing it
poll()        E Retrieves and removes the head of the deque
push(E e)        voidPushes an element onto the stack represented by the deque
pop()      EPops an element from the stack represented by the deque.
remove()         ERetrieves and removes the head of the queue
size()     intReturns the size of the deque
 toArray()    Object[]Returns an array containing all of the elements in the deque

Implementation:

Example

Java




// Java Program to Implement ConcurrentLinkedDeque API
  
// Importing all classes from
// java.util package
import java.util.*;
import java.util.concurrent.*;
  
// Class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Object 1
        // Create a ConcurrentLinkedDeque object
        // Declaring object of Integer type
        ConcurrentLinkedDeque<Integer> dq
            = new ConcurrentLinkedDeque<Integer>();
  
        // Adding element to the front
        // using addFirst() method
        // Custom entry
        dq.add(89);
  
        // Adding an element in the last
        // using addLast() method
        // Custom entry
        dq.addLast(18);
  
        // Adding an element to the front
        // Custom inputs
        dq.addFirst(10);
        dq.add(45);
  
        // Displaying the current ConcurrentLinkedDeque
        System.out.println("ConcurrentLinkedDeque1 : "
                           + dq);
  
        // Object 2
        // Creating a ConcurrentLinkedDeque object
        // using ConcurrentLinkedDeque(Collection c)
        // Declaring object of Integer type
        ConcurrentLinkedDeque<Integer> ldq
            = new ConcurrentLinkedDeque<Integer>(dq);
  
        // Displaying the current ConcurrentLinkedDeque
        System.out.println("ConcurrentLinkedDeque2 : "
                           + ldq);
  
        // Print the size of the deque
        // using size() method
        System.out.println("Size: " + ldq.size());
  
        // Removing all the elements from the deque
        // using clear() method
        ldq.clear();
  
        // Checking whether the ConcurrentLinkedDeque object
        // is empty or not
        System.out.println("Is Deque empty: "
                           + ldq.isEmpty());
  
        // Removing the head of deque of object1
        dq.remove();
  
        // Iterating over elements and
        // printing deque of object1
        Iterator it = dq.iterator();
  
        // Condition check using hasNext() which hold
        // true till single element remaining in List
        while (it.hasNext())
  
            // Print all the elements
            System.out.print(it.next() + " ");
    }
}

Output

ConcurrentLinkedDeque1: [10, 89, 18, 45]
ConcurrentLinkedDeque2: [10, 89, 18, 45]
Size: 4
Is Deque empty: true
89 18 45 

Last Updated : 19 Jan, 2021
Like Article
Save Article
Similar Reads
Related Tutorials