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 :
- public ConcurrentLinkedDeque(): It creates an empty deque.
- public ConcurrentLinkedDeque(Collection<E> c): It creates a deque that initially contains the elements of the Collection<E>.
Methods:
Method | Type | Description |
---|
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() | void | Removes 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() | E | Retrieves 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) | void | Pushes an element onto the stack represented by the deque |
pop() | E | Pops an element from the stack represented by the deque. |
remove() | E | Retrieves and removes the head of the queue |
size() | int | Returns the size of the deque |
toArray() | Object[] | Returns an array containing all of the elements in the deque |
Implementation:
Example
Java
import java.util.*;
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
ConcurrentLinkedDeque<Integer> dq
= new ConcurrentLinkedDeque<Integer>();
dq.add( 89 );
dq.addLast( 18 );
dq.addFirst( 10 );
dq.add( 45 );
System.out.println( "ConcurrentLinkedDeque1 : "
+ dq);
ConcurrentLinkedDeque<Integer> ldq
= new ConcurrentLinkedDeque<Integer>(dq);
System.out.println( "ConcurrentLinkedDeque2 : "
+ ldq);
System.out.println( "Size: " + ldq.size());
ldq.clear();
System.out.println( "Is Deque empty: "
+ ldq.isEmpty());
dq.remove();
Iterator it = dq.iterator();
while (it.hasNext())
System.out.print(it.next() + " " );
}
}
|
OutputConcurrentLinkedDeque1: [10, 89, 18, 45]
ConcurrentLinkedDeque2: [10, 89, 18, 45]
Size: 4
Is Deque empty: true
89 18 45