Open In App

Java Program to Implement ConcurrentLinkedDeque API

Last Updated : 19 Jan, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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


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

Next Article

Similar Reads