- ArrayDeque is one of the collection framework member which implements Deque, Cloneable and Serializable interfaces.
- ArrayDeque provides Resizable-array implementation so it has no capacity restrictions and it will grow as per requirement.
- In the absence of external synchronization, they are not thread safe. So they do not allow multiple threads to access it concurrently.
- Also ArrayDequenot allow to store null elements.
- ArrayDeque Class is faster than LinkedList when used as a queue and faster than Stack when used as a stack.
- If ArrayDeque is modified after the iterator creation and if it is not modified by iterators own method then it will throw ConcurrentModificationException. So in concurrent modification, Iterator will fail.
- boolean add(E e) : It will insert given element in ArrayDeque.
- void addFirst(E e) : It will add given element at the beginning of ArrayDeque.
- void addLast(E e) : It will add given element at the end of ArrayDeque.
- void clear() : It will remove all elements from ArrayDeque to clear it.
- ArrayDeque<E> clone() : It will return copy of ArrayDeque.
- boolean contains(Object o) : It will return true if specified element available in ArrayDeque.
- Iterator<E> descendingIterator() : It will return iterator in reverse sequential order over the elements of ArrayDeque.
- E element() : It will get head(first) element from this ArrayDeque.
- E getFirst() : It will get head(first) element from this ArrayDeque.
- E getLast() : It will get last element from this ArrayDeque.
- boolean isEmpty() : It will return true if ArrayDeque is empty.
- boolean offer(E e) : It will insert specified element at the end of this ArrayDeque.
- boolean offerFirst(E e) : It will insert specified element at the beginning of this ArrayDeque.
- boolean offerLast(E e) : It will insert specified element at the end of this ArrayDeque.
- E peek() : It will retrieve and return head element from ArrayDeque. Return null if ArrayDeque is empty.
- E peekFirst() : It will retrieve and return head(first) element from ArrayDeque. Return null if ArrayDeque is empty.
- E peekLast() : It will retrieve and return last element from ArrayDeque. Return null if ArrayDeque is empty.
- E poll() : It will remove head element from ArrayDeque. Return null if ArrayDeque is empty.
- E pollFirst() : It will remove head(first) element from ArrayDeque. Return null if ArrayDeque is empty.
- E pollLast() : It will remove last element from ArrayDeque. Return null if ArrayDeque is empty.
- E pop() : It will pop an element from stack represented by this ArrayDeque.
- void push(E e) : It will pushes an element on stack represented by this ArrayDeque.
- E remove() : It will remove head element of the ArrayDeque.
- boolean remove(Object o) : It will remove single instance of specified object from ArrayDeque.
- E removeFirst() : It will remove first element from ArrayDeque.
- boolean removeFirstOccurrence(Object o) : It will remove first occurrence of the element from ArrayDeque.
- E removeLast() : It will remove last element from ArrayDeque.
- boolean removeLastOccurrence(Object o) : It will remove last occurrence of the element from ArrayDeque.
- int size() :It will return size of ArrayDeque.
- Object[] toArray() : It will return elements array from ArrayDeque.
- <T> T[] toArray(T[] a) : It will return elements array from ArrayDeque. The runtime type of the returned array is that of the specified array.
Below given example will show you demo of above ArrayDeque method's usage.
ArrayDeque Example
package JAVAExamples;
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeExample {
public static void main(String[] args) {
// Create ArrayDeque.
ArrayDeque<String> d = new ArrayDeque<String>();
// Add elements in Deque.
d.add("one");
d.add("two");
d.add("three");
d.add("four");
d.add("five");
d.add("four");
d.add("six");
// Print Deque elements.
System.out.println("Deque elements are : " + d);
// Add element at first of Deque.
d.addFirst("First Added");
System.out.println("Deque elements after addFirst are : " + d);
// Add element at last of Deque.
d.addLast("Last Added");
System.out.println("Deque elements after addLast are : " + d);
// Get first element from Deque.
System.out.println("First element in deque is : " + d.getFirst());
// Get last element from Deque.
System.out.println("Last element in deque is : " + d.getLast());
// Get first element from Deque using peek.
System.out.println("First element in deque using peek is : " + d.peek());
// Get last element from Deque using peekLast.
System.out.println("Last element in deque using peekLast is : " + d.peekLast());
// Remove first element from deque.
d.removeFirst();
System.out.println("Deque elements after removeFirst are : " + d);
// Using pop.
d.pop();
System.out.println("Deque elements after pop are : " + d);
// Using push.
d.push("First");
System.out.println("Deque elements after push are : " + d);
// Remove LastOccurrence of element from deque using removeLastOccurrence.
d.removeLastOccurrence("four");
System.out.println("Deque elements after removeLastOccurrence are : "+ d);
// Get size of deque.
System.out.println("Size of Deque is : " + d.size());
}
}
Output :
Deque elements are : [one, two, three, four, five, four, six]
Deque elements after addFirst are : [First Added, one, two, three, four, five, four, six]
Deque elements after addLast are : [First Added, one, two, three, four, five, four, six, Last Added]
First element in deque is : First Added
Last element in deque is : Last Added
First element in deque using peek is : First Added
Last element in deque using peekLast is : Last Added
Deque elements after removeFirst are : [one, two, three, four, five, four, six, Last Added]
Deque elements after pop are : [two, three, four, five, four, six, Last Added]
Deque elements after push are : [First, two, three, four, five, four, six, Last Added]
Deque elements after removeLastOccurrence are : [First, two, three, four, five, six, Last Added]
Size of Deque is : 7
No comments:
Post a Comment