ArrayDeque and its Methods
Before discussing about ArrayDeque let us look at the Collection Hierarchy.
Collection Hierarchy
The below diagram shows the Collection hierarchy consisting of classes and interfaces.
Now let us discuss about ArrayDeque class and its methods
What is ArrayDeque ?
ArrayDeque is a interface which stores all Collection elements. It is the implemented class of Deque interface,Queue interface,Collection interface and Iterable interface. It is present in java.utils package.ArrayDeque follow indexing. It is not thread safe and not synchronized. ArrayDeque can double its size and its threshold is that its current capacity get exceeded. Elements are stored in ArrayDeque using resizeable array which means it can increase or decrease its size depending on the number of elements to be stored. In ArrayDeque elements are added and removed from both sides of the queue.It provides a constant time performance for adding or removing elements thus making it best suited for add and remove operations.There is no capacity restrictions in ArrayDeque.It does not allow any null values.
Methods of ArrayDeque class
Below are some of the methods of ArrayDeque class:
Method Name | Description |
add(Object e) | This method is used to add a new element into the ArrayDeque class. |
addAll(Collection c) | This method is used to add all elements into the ArrayDeque class. |
clear() | It is used to clear all the elements from theArrayDeque class but it does not clear theArrayDeque class. |
contains(Object o) | This method is used to check the presence of a Collection element in theArrayDeque. It returns ‘true’ value if the element is present in ArrayDeque and ‘false’ value if the element is not present in the ArrayDeque. |
containsAll(Collection c) | This method is used to check if the ArrayDeque contains all the collection elements. It returns ‘true’ value if all the elements are present in ArrayDeque and ‘false’ value if any one of the element is not present in the ArrayDeque. |
isEmpty() | This method checks if the given ArrayDeque is empty |
iterator() | This method is used to iterate sequentially all the Collection elements present in an ArrayDeque. |
remove(Object o) | This method is used to remove a specified collection element from the ArrayDeque. |
removeAll(Collection c) | This method removes all those Collection elements from the current ArrayDeque which are present in another ArrayDeque. |
retainAll(Collection c) | This method retains all those elements from the current ArrayDeque which are present in another ArrayDeque and removes those elements which are present in current ArrayDeque but not in another ArrayDeque and vice versa. |
size() | This method is used to find out and display the number of Collection elements present in the ArrayDeque |
toArray() | This method is used to convert all collection elements present in ArrayDeque into an array. |
toString() | This method is used to convert all collection elements present in ArrayDeque into a String. |
offer(Object e) | This method is used to add new elements into the ArrayDeque class. |
poll() | This method is used to remove the topmost element of the ArrayDeque which is displayed in output console. |
peek() | This method is used to fetch or point out the topmost element of the ArrayDeque which is displayed in output console but does not remove that element from the ArrayDeque class. |
element() | This method is used to fetch or point out the topmost element of the ArrayDeque which is displayed in output console but does not remove that element from the ArrayDeque class. |
addFirst(Object e) | It is used to add an element at the beginning of ArrayDeque. |
addLast(Object e) | It is used to add an element at the end of ArrayDeque. |
getFirst() | It is used to display the first element of ArrayDeque class. |
getLast() | It is used to display the last element of ArrayDeque class. |
removeFirst() | It is used to remove the first element of ArrayDeque class. |
removeLast() | It is used to remove the last element of ArrayDeque class. |
pollFirst() | It is used to remove the first element of ArrayDeque class. |
pollLast() | It is used to remove the last element of ArrayDeque class. |
descendingIterator() | This method is used to iterate all the Collection elements present in ArrayDeque in descending order. |
peekFirst() | It is used to display the first element of ArrayDeque class. |
peekLast() | It is used to display the last element of ArrayDeque class. |
offerFirst(Object e) | It is used to add an element at the beginning of ArrayDeque. |
offerLast(Object e) | It is used to add an element at the end of ArrayDeque. |
clone() | This method is used to create a shallow copy of the elements present in the ArrayDeque class. |
1.Let us check how add(Object e) method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
System.out.println(a1);
}
}
Output: [Hi, Hey, Hello, Hji, 23, 34]
Screenshot:
2. Let us check how addAll(Collection c) works in ArrayDeque class.
  Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
ArrayDeque a2= new ArrayDeque();
a2.addAll(a1);
System.out.println(a2);
}
}
Output: [Hi, Hey, Hello, Hji, 23, 34]
Screenshot:
3. Let us check how clear() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
a1.clear();
System.out.println(a1);
}
}
Output: []
Screenshot:
4. Let us check how contains(Object o) method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
System.out.println("The given ArrayDeque contains Hello: "+a1.contains("Hello"));
System.out.println("The given ArrayDeque contains Bye: "+a1.contains("bye"));
}
}
Output: The given ArrayDeque contains Hello: true
The given ArrayDeque contains Bye: false
Screenshot:
5. Let us check how containsAll(Collection c) method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
ArrayDeque a2= new ArrayDeque();
a2.add("Hi");
a2.add("Hey");
a2.add("Hello");
a2.add("Hji");
System.out.println("Does a2 contains all elements of a1:"+a2.containsAll(a1));
System.out.println("Does a1 contains all elements of a2:"+a1.containsAll(a2));
}
}
Output: Does a2 contains all elements of a1:false
Does a1 contains all elements of a2:true
Screenshot:
6. Let us check how isEmpty() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
ArrayDeque a2= new ArrayDeque();
System.out.println("Is a1 ArrayDeque empty?: "+a1.isEmpty());
System.out.println("Is a2 ArrayDeque empty?: "+a2.isEmpty());
}
}
Output: Is a1 ArrayDeque empty?: false
Is a2 ArrayDeque empty?: true
Screenshot:
7. Let us check how iterator() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
import java.util.Iterator;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
Iterator a2= a1.iterator();
System.out.println("The collection elements values are: ");
while(a2.hasNext())
{
System.out.println(a2.next());
}
}
}
Output: The collection elements values are:
Hi
Hey
Hello
Hji
23
34
Screenshot:
8. Let us check how remove(Object o) method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
a1.remove("Hello");
a1.remove("Hji");
System.out.println(a1);
}
}
Output: [Hi, Hey, 23, 34]
Screenshot:
9. Let us check how removeAll(Collection c) works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
ArrayDeque a2= new ArrayDeque();
a2.add("Hi");
a2.add("Hey");
a2.add("Hello");
a1.removeAll(a2);
System.out.println("Elements present in a1 after removing a2 elements from a1 are:"+a1);
}
}
Output: Elements present in a1 after removing a2 elements from a1 are:[Hji, 23, 34]
Screenshot:
10. Let us check how retainAll(Collection c) works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
ArrayDeque a2= new ArrayDeque();
a2.add("Hi");
a2.add("Hey");
a2.add("Hello");
a1.retainAll(a2);
System.out.println("Elements present in a1 after retaining a2 elements are:"+a1);
}
}
Output: Elements present in a1 after retaining a2 elements are:[Hi, Hey, Hello]
Screenshot:Â
11. Let us check how size() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
System.out.println("The size of the collection elements present in the ArrayDeque is: "+a1.size());
}
}
Output: The size of the collection elements present in the ArrayDeque is: 6
Screenshot:
12. Let us check how toArray() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
Object a2[]= a1.toArray();
System.out.println("The list of array elements are:");
for(int i=0;i<=a2.length-1;i++)
{
System.out.println(a2[i]);
}
}
}
Output:
The list of array elements are:
Hi
Hey
Hello
Hji
23
34
Screenshot:Â
13. Let us check how toString() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
System.out.println(a1.toString());
}
}
Output: [Hi, Hey, Hello, Hji, 23, 34]
Screenshot:
14. Let us check how offer(Object e) method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.offer("Hi");
a1.offer("Hey");
a1.offer("Hello");
a1.offer("Hji");
a1.offer(23);
a1.offer(34);
System.out.println(a1);
}
}
Output: [Hi, Hey, Hello, Hji, 23, 34]
Screenshot:
15. Let us check how poll() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
System.out.println("Initial List of ArrayDeque elements: "+a1);
System.out.println("Removed Element is: "+a1.poll());
System.out.println("New List of ArrayDeque elements after using poll method: "+a1);
}
}
Output: Initial List of ArrayDeque elements: [Hi, Hey, Hello, Hji, 23, 34]
Removed Element is: Hi
New List of ArrayDeque elements after using poll method: [Hey, Hello, Hji, 23, 34]
Screenshot:
16. Let us check how peek() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
System.out.println("Initial List of ArrayDeque elements: "+a1);
System.out.println("Element on which peek method is applied: "+a1.peek());
System.out.println("New List of ArrayDeque elements after using peek method: "+a1);
}
}
Output:
Initial List of ArrayDeque elements: [Hi, Hey, Hello, Hji, 23, 34]
Element on which peek method is applied: Hi
New List of ArrayDeque elements after using peek method: [Hi, Hey, Hello, Hji, 23, 34]
Screenshot:
17. Let us check how element() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
System.out.println("Initial List of ArrayDeque elements: "+a1);
System.out.println("Element on which element method is applied: "+a1.element());
System.out.println("New List of ArrayDeque elements after using element method: "+a1);
}
}
Output: Initial List of ArrayDeque elements: [Hi, Hey, Hello, Hji, 23, 34]
Element on which element method is applied: Hi
New List of ArrayDeque elements after using element method: [Hi, Hey, Hello, Hji, 23, 34]
Screenshot:
18. Let us check how addFirst(Object e) method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
a1.addFirst(345);
System.out.println(a1);
}
}
Output: [345, Hi, Hey, Hello, Hji, 23, 34]
Screenshot:
19. Let us check how addLast(Object e) method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
a1.addLast(345);
System.out.println(a1);
}
}
Output: [Hi, Hey, Hello, Hji, 23, 34, 345]
Screenshot:
20. Let us check how getFirst() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
System.out.println(a1.getFirst());
}
}
Output: Hi
Screenshot:
21. Let us check how getLast() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
System.out.println(a1.getLast());
}
}
Output: 34
Screenshot:
22. Let us check how removeFirst() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
System.out.println("Element which is removed: "+a1.removeFirst());
System.out.println("Remaining elements of ArrayDeque: "+a1);
}
}
Output: Element which is removed: Hi
Remaining elements of ArrayDeque: [Hey, Hello, Hji, 23, 34]
Screenshot:
23. Let us check how removeLast() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
System.out.println("Element which is removed: "+a1.removeLast());
System.out.println("Remaining elements of ArrayDeque: "+a1);
}
}
Output: Element which is removed: 34
Remaining elements of ArrayDeque: [Hi, Hey, Hello, Hji, 23]
Screenshot:
24. Let us check how pollFirst() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
System.out.println("Element which is removed: "+a1.pollFirst());
System.out.println("Remaining elements of ArrayDeque: "+a1);
}
}
Output:
Element which is removed: Hi
Remaining elements of ArrayDeque: [Hey, Hello, Hji, 23, 34]
Screenshot:
25. Let us check how pollLast() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add("Hi");
a1.add("Hey");
a1.add("Hello");
a1.add("Hji");
a1.add(23);
a1.add(34);
System.out.println("Element which is removed: "+a1.pollLast());
System.out.println("Remaining elements of ArrayDeque: "+a1);
}
}
Output: Element which is removed: 34
Remaining elements of ArrayDeque: [Hi, Hey, Hello, Hji, 23]
Screenshot:
26. Let us check how descendingIterator() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
import java.util.Iterator;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add(12);
a1.add(18);
a1.add(16);
a1.add(20);
a1.add(23);
a1.add(34);
Iterator a2= a1.descendingIterator();
System.out.println("The collection elements values in descending order are: ");
while(a2.hasNext())
{
System.out.println(a2.next());
}
}
}
Output:
The collection elements values in descending order are:
34
23
20
16
18
12
Screenshot:
27. Let us check how peekFirst() method works in ArrayDeque class.
Code Snippet:Â
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add(12);
a1.add(18);
a1.add(16);
a1.add(20);
a1.add(23);
a1.add(34);
System.out.println(a1.peekFirst());
}
}
Output: 12
Screenshot:
28. Let us check how peekLast() method works in ArrayDeque class.
Code Snippet:Â
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add(12);
a1.add(18);
a1.add(16);
a1.add(20);
a1.add(23);
a1.add(34);
System.out.println(a1.peekLast());
}
}
Output: 34
Screenshot:
29. Let us check how offerFirst(Object e) method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.offer(12);
a1.offer(18);
a1.offer(16);
a1.offer(20);
a1.offer(23);
a1.offer(34);
a1.offerFirst(32);
System.out.println(a1);
}
}
Output: [32, 12, 18, 16, 20, 23, 34]
Screenshot:
30. Let us check how offerLast(Object e) method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.offer(12);
a1.offer(18);
a1.offer(16);
a1.offer(20);
a1.offer(23);
a1.offer(34);
a1.offerLast(32);
System.out.println(a1);
}
}
Output: [12, 18, 16, 20, 23, 34, 32]
Screenshot:
31. Let us check how clone() method works in ArrayDeque class.
Code Snippet:
package arrayDeque;
import java.util.ArrayDeque;
public class ArrayDequeClass {
public static void main(String[] args) {
ArrayDeque a1= new ArrayDeque();
a1.add(12);
a1.add(18);
a1.add(16);
a1.add(20);
a1.add(23);
a1.add(34);
System.out.println("List of elements in ArrayDeque class: "+a1);
ArrayDeque clonea1= new ArrayDeque();
clonea1=(ArrayDeque)a1.clone();
System.out.println("List of elements after using clone method:"+ clonea1);
}
}
Output: List of elements in ArrayDeque class: [12, 18, 16, 20, 23, 34]
List of elements after using clone method:[12, 18, 16, 20, 23, 34]
Screenshot:
Difference between Queue and ArrayDeque
SL NO: | Queue | ArrayDeque |
1 | It is an interface | It is a class |
2 | It is the child interface of Collection interface and grandchild of Iterable interface. | It is the implemented class of Deque interface,Queue interface,Collection interface and Iterable interface. |
3 | Does not follow indexing | Follows indexing |
Conclusion:
ArrayDeque class is used for handling all the Collection elements in a proper manner. It is necessary to understand the methods of ArrayDeque for better handling of Collection elements. With in-depth understanding of the ArrayDeque class and its methods result in developers and testers utlilising these concepts in real life tasks. Remember to practise, stay updated with the latest trends in Automation Software Testing Course,and maintain a positive attitude throughout your interview process. Developers could write better program codes for software applications and Testers could write better test scripts for testing the software applications due to usage of ArrayDeque class and its methods.
Read my blogs on :