ArrayDeque in Java
ArrayDeque in Java provides a way to apply resizable-array in addition to the implementation of the Deque interface. It is also known as Array Double Ended Queue or Array Deck. This is a special kind of array that grows and allows users to add or remove an element from both sides of the queue. Few important features of ArrayDeque are as follows:
- Array deques have no capacity restrictions and they grow as necessary to support usage.
- They are not thread-safe which means that in the absence of external synchronization, ArrayDeque does not support concurrent access by multiple threads.
- Null elements are prohibited in the ArrayDeque.
- ArrayDeque class is likely to be faster than Stack when used as a stack.
- ArrayDeque class is likely to be faster than LinkedList when used as a queue.
Declaration:
public class ArrayDeque Element extends AbstractCollection implements DequeElement, Cloneable, Serializable
Here, Element refers to the element which can refer to any class, such as Integer or String class.
Constructors in ArrayDeque:
- ArrayDeque(): Used to create an empty ArrayDeque and by default holds an initial capacity to hold 16 elements.
- ArrayDeque(Collection c): Used to create an ArrayDeque containing all the elements the same as that of the specified collection.
- ArrayDeque(int num elements): Used to create an empty ArrayDeque and holds the capacity to contain a specified number of elements.
Example:
filter_none
edit
play_arrow
brightness_4
|
Output:
Element : 10 Element : 20 Element : 30 Element : 40 Element : 50 Using clear() Above elements are removed now Elements of deque using Iterator : 291 564 24 14 Elements of deque in reverse order : 14 24 564 291 Head Element using element(): 291 Head Element using getFirst(): 291 Last Element using getLast(): 14 Array Size : 4 Array elements : 291 564 24 14 Head element : 291 Head element poll : 291 Head element remove : 2365 The final array is: [984, 265, 564, 24, 14]
Methods in ArrayDeque:
- add(Element e): The method inserts a particular element at the end of the deque.
- and first(Element e): The method inserts a particular element at the start of the deque.
- addLast(Element e): The method inserts a particular element at the end of the deque. It is similar to add() method
- clear() : The method removes all deque elements.
- size(): The method returns the no. of elements in the deque.
- clone() : The method copies the deque.
- contains(Obj): The method checks whether a deque contains the element or not
- Iterator(): The method returns an iterator over the deque.
- descendingIterator(): The method returns a reverse order iterator over the deque
- element(): The method returns the element at the head of the deque
- net first(): The method returns the first element of the deque
- get last(): The method returns the last element of the deque
- isEmpty(): The method checks whether the deque is empty or not.
- toArray(): The method returns array having the elements of deque.
- offer(Element e): The method inserts element at the end of the deque.
- offer first(Element e): The method inserts an element at the front of the deque.
- offerLast(Element e): The method inserts element at the end of the deque.
- peek(): The method returns the head element without removing it.
- seek first(): The method returns the first element without removing it.
- peekLast(): The method returns the last element without removing it.
- poll(): The method returns head element and also removes it
- pollFirst(): The method returns the first element and also removes it
- poll last(): The method returns the last element and also removes it
- pop(): The method pops out an element for stack represented by deque
- push(Element e): The method pushes an element onto stack represented by deque
- remove(): The method returns head element and also removes it
- remove first(): The method returns the first element and also removes it
- removes last(): The method returns the last element and also removes it
- remove first occurrence(Obj): The method removes the element where it first occurs in the deque.
- remove last occurrence(Obj): The method removes the element where it last occurs in the deque.
Related Articles:
- Java.util.ArrayDeque Class in Java | Set 1
- Java.util.ArrayDeque Class in Java | Set 2
post a comment