ArrayDeque in Java

ArrayQueue

In the previous blog, we learned about the Dequeue Interface. If you want to know more about it visit Dequeue Interface in Java. In this blog, we will go through another interface of Queue i.e. ArrayDeque.


ArrayDeque Interface

ArrayDeque stands for Array Double-Ended Queue. ArrayDeque grows and allows users to add or remove an element from both sides of the queue. ArrayDeque is present in java. util.ArrayDeque.


Syntax of ArrayDeque

public class ArrayDeque<> extends AbstractCollection<> implements Deque<>, Cloneable, Serializable

Methods of ArrayDeque

  1. add()
  2. add() method adds the specified element at the end of the array deque.

  3. addFirst()
  4. addFirst() method adds the specified element at the start of the array deque.

  5. addLast()
  6. addLast() method adds the specified at the end of the array deque (same as add()).

  7. offer()
  8. offer() method adds the specified element at the end of the array deque.

  9. offerFirst()
  10. offerFirst() method adds the specified element at the start of the array deque.

  11. offerLast()
  12. offerLast() appends the specified element to the end of the array deque.

    Example of ArrayDeque using the above three methods

    import java.util.ArrayDeque;
    class ArrayDequeOfferExample {
      public static void main(String[] args) {
          ArrayDeque<Integer> str= new ArrayDeque<>();
          str.offer(10);
          str.offerFirst(50);
          str.offerLast(90);
          System.out.println("ArrayDeque: " + str);
      }
    }

    Output

    ArrayDeque: [50, 10, 90]
  13. peek()
  14. peek() method returns the first element of the array deque.

  15. peekFirst()
  16. peekFirst() method returns the first element of the array deque (same as peek()).

  17. peekLast()
  18. peekLast() method returns the last element of the array deque.

  19. getFirst()
  20. The first element of the array deque is returned by the getFirst() method.getFirst() method returns the first element of the array deque.

  21. getLast()
  22. getLast() method returns the last element of the array deque.

    Example of ArrayDeque using the above methods

    import java.util.ArrayDeque;
    class ArrayDequePeekExample {
      public static void main(String[] args) {
          ArrayDeque<String> str= new ArrayDeque<>();
          str.add("Java");
          str.addFirst("React");
          str.addLast("JS");
          System.out.println("ArrayDeque: " + str);
          System.out.println("The Head Element is: " + str.peek());
          System.out.println("The First Element is: " + str.peekFirst());
          System.out.println("The Last Element is: " + str.peekLast());
          System.out.println("The First Element is: " + str.getFirst());
          System.out.println("The Last Element is: " + str.getLast());
    
      }
    }

    Output

    ArrayDeque: [React, Java, JS]
    The Head Element is: React
    The First Element is: React
    The Last Element is: JS
    The First Element is: React
    The Last Element is: JS
  23. remove()
  24. remove() method returns and removes an element from the first element of the array deque

  25. removeFirst()
  26. removeFirst() method returns and removes the first element from the array deque (same as remove())

  27. removeLast() method returns and removes the last element from the array deque

  28. poll()
  29. poll() method returns and removes the first element of the array deque

  30. pollFirst()
  31. pollFirst() method returns and removes the first element of the array deque (same as poll())

  32. pollLast()
  33. pollLast() method returns and removes the last element of the array deque

    Example of ArrayDeque using the above mentioned methods

    import java.util.ArrayDeque;
    class ArrayDequeRemovePollExample {
      public static void main(String[] args) {
        ArrayDeque<String> str= new ArrayDeque<>();
        str.add("Canada");
        str.add("London");
        str.add("New York");
        System.out.println("ArrayDeque: " + str);
        System.out.println("ArrayDeque using poll() method: " + str.poll());
        System.out.println("After poll() method ArrayDeque is: " + str);
        System.out.println("ArrayDeque using pollFirst() method: " + str.pollFirst());
        System.out.println("After pollFirst() method ArrayDeque is: " + str);
        System.out.println("ArrayDeque using pollLast() method: " + str.pollLast());
        System.out.println("After pollLast() method ArrayDeque is: " + str);
    
        ArrayDeque<String> str1= new ArrayDeque<>();
        str1.add("Spain");
        str1.add("Malasiya");
        str1.add("California");
        str1.add("London");
        System.out.println("ArrayDeque2: " + str1);
        System.out.println("ArrayDeque2 using remove() method: " + str1.remove());
        System.out.println("After remove() method ArrayDeque2 is: " + str1);
        System.out.println("ArrayDeque2 using removeFirst() method: " + str1.removeFirst());
        System.out.println("After removeFirst() method ArrayDeque2 is: " + str1);
        System.out.println("ArrayDeque2 using removeLast() method: " + str1.removeLast());
        System.out.println("After removeLast() method ArrayDeque2 is: " + str1);
    
      }
    }

    Output

    ArrayDeque: [Canada, London, New York]
    ArrayDeque using poll() method: Canada
    After poll() method ArrayDeque is: [London, New York]
    ArrayDeque using pollFirst() method: London
    After pollFirst() method ArrayDeque is: [New York]
    ArrayDeque using pollLast() method: New York
    After pollLast() method ArrayDeque is: []
    ArrayDeque2: [Spain, Malasiya, California, London]
    ArrayDeque2 using remove() method: Spain
    After remove() method ArrayDeque2 is: [Malasiya, California, London]
    ArrayDeque2 using removeFirst() method: Malasiya
    After removeFirst() method ArrayDeque2 is: [California, London]
    ArrayDeque2 using removeLast() method: London
    After removeLast() method ArrayDeque2 is: [California]
Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.