ConcurrentLinkedDeque in Java with Examples
The ConcurrentLinkedDeque class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. It is used to implement Deque with the help of LinkedList concurrently.
Class Hierarchy:
java.lang.Object ? java.util.AbstractCollection<E> ? Class ConcurrentLinkedDeque<E>
Syntax:
public abstract class ConcurrentLinkedDeque<E> extends AbstractCollection<E> implements Deque<E>, Serializable Where E is the type of elements maintained by this collection.
Constructors in Java ConcurrentLinkedDeque:
- ConcurrentLinkedDeque(): This constructor is used to construct an empty deque.
- ConcurrentLinkedDeque(Collection<E> c): This constructor is used to construct a deque with the elements of the Collection passed as the parameter.
Below is the sample program to illustrate ConcurrentLinkedDeque in Java:
filter_none
edit
play_arrow
brightness_4
|
Output:
ConcurrentLinkedDeque: [475, 1009, 70, 12] ConcurrentLinkedDeque1: [475, 1009, 70, 12]
Methods in Java ConcurrentLinkedDeque:
- add(E e): This method Inserts the specified element at the tail of this deque.
- addAll(Collection c): This method Appends all of the elements in the specified collection to the end of this deque, in the order that they are returned by the specified collection’s iterator.
- and first(E e): This method Inserts the specified element at the front of this deque.
- addLast(E e): This method Inserts the specified element at the end of this deque.
- clear(): This method Removes all of the elements from this deque.
- contains(Object o): This method returns true if this deque contains at least one element e such that o.equals(e).
- descendingIterator(): This method returns an iterator over the elements in this deque in reverse sequential order.
- element(): This method Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque).
- net first(): This method Retrieves, but does not remove, the first element of this deque.
- get last(): This method Retrieves but does not remove, the last element of this deque.
- isEmpty(): This method returns true if this collection contains no elements.
- iterator(): This method returns an iterator over the elements in this deque in a proper sequence.
- offer(E e): This method Inserts the specified element at the tail of this deque.
- offer first(E e): This method Inserts the specified element at the front of this deque.
- offerLast(E e): This method Inserts the specified element at the end of this deque.
- peek(): This method Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.
- seek first(): This method Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
- peekLast(): This method Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
- poll(): This method Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.
- pollFirst(): This method Retrieves and removes the first element of this deque, or returns null if this deque is empty.
- poll last(): This method Retrieves and removes the last element of this deque, or returns null if this deque is empty.
- pop(): This method Pops an element from the stack represented by this deque.
- push(E e): This method Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
- remove(): This method Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque).
- remove(Object o): This method removes the first element e such that o.equals(e), if such an element exists in this deque.
- remove first(): This method Retrieves and removes the first element of this deque.
- remove first occurrence(Object o): This method removes the first element e such that o.equals(e), if such an element exists in this deque.
- remove last(): This method Retrieves and removes the last element of this deque.
- remove last occurrence(Object o): This method removes the last element e such that o.equals(e), if such an element exists in this deque.
- size(): This method returns the number of elements in this deque.
- toArray(): This method returns an array containing all of the elements in this deque, in proper sequence (from first to the last element).
- toArray(T[] a): This method returns an array containing all of the elements in this deque, in proper sequence (from first to the last element); the runtime type of the returned array is that of the specified array.
Example:
filter_none
edit
play_arrow
brightness_4
|
Output:
ConcurrentLinkedDeque: [475, 1009, 70, 12] The Last element is: 12 First Element is: 475 ConcurrentLinkedDeque: [475, 1009, 70]
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html
post a comment