MOCKSTACKS
EN
Questions And Answers

More Tutorials









Java ArrayDeque


ArrayDeque = Resizable array + Deque interface.
ArrayDeque implements the Queue & Deque interface.
There are no capacity restrictions for ArrayDeque, and it provides us the facility to add or remove any element from both sides of the queue.
Also known as Array Double Ended Queue.
It is faster than Linked list and stack.

Constructors of ArrayDeque class :

1. ArrayDeque(): Used to create an empty array deque that has the capacity to hold 16 elements.
2. ArrayDeque(int numElements): Used to create an empty array deque that has the capacity to hold the specified number of elements.
3. ArrayDeque(Collection c): Used to create an array deque containing all the elements of the specified collections.

Performing Various Operation On ArrayDeque() :


1. Inserting an element :


Insertion at front : add(), offerFirst() and addFirst() methods are used to insert an element at front of an array deque.

Example :
import java.util.*;
public class CWH extends Thread{
    public static void main(String[] args) {

        ArrayDeque<Integer> ad1 = new ArrayDeque<>();
        ad1.add(6);
        ad1.add(56);
        ad1.add(9);
        ad1.addFirst(5);
        ad1.offerFirst(10);
        System.out.println(ad1);

    }
}

Output

[10, 5, 6, 56, 9]

  • Insertion At End:

  • addLast() and offerLast() methods are used to insert an element at the end of the array deque.
    Example :
    import java.util.*;
    public class CWH extends Thread{
        public static void main(String[] args) {
    
            ArrayDeque<Integer> ad1 = new ArrayDeque<>();
            ad1.add(6);
            ad1.add(56);
            ad1.add(9);
            ad1.addLast(5);
            ad1.offerLast(10);
            
            System.out.println(ad1);
    
        }
    }
    

    Output

    [6, 56, 9, 5, 10]

    2. Accessing an element :


    Accessing an element from the head of the deque array: getFirst() & peekFirst() methods are used to get the first element of the deque array.
    Example :
    import java.util.*;
    public class CWH extends Thread{
        public static void main(String[] args) {
    
            ArrayDeque<Integer> ad1 = new ArrayDeque<>();
            ad1.add(6);
            ad1.add(56);
            ad1.add(9);
            ad1.add(10);
            ad1.add(91);
            ad1.add(19);
    
            System.out.println(ad1.getFirst());
            System.out.println(ad1.peekFirst());
    
        }
    }
    

    Output

    6
    6

  • Accessing the last element:

  • getLast() or peekLast() methods are used to print the last element of the deque array.
    Example :
    import java.util.*;
    public class CWH extends Thread{
        public static void main(String[] args) {
    
            ArrayDeque<Integer> ad1 = new ArrayDeque<>();
            ad1.add(6);
            ad1.add(56);
            ad1.add(9);
            ad1.add(10);
            ad1.add(91);
            ad1.add(19);
    
            System.out.println(ad1.getLast());
            System.out.println(ad1.peekLast());
    
        }
    }
    

    Output

    19
    19

    3. Removing an element :


    Removing the first element: removeFirst() & pollFirst() methods are used to delete an element from the head of the queue.
    removeFirst() throws an exception if the queue is empty.
    pollFirst() returns null if the queue is empty.
    Example :
    import java.util.*;
    public class CWH extends Thread{
        public static void main(String[] args) {
    
            ArrayDeque<Integer> ad1 = new ArrayDeque<>();
            ad1.add(6);
            ad1.add(56);
            ad1.add(9);
            ad1.add(10);
            ad1.add(91);
            ad1.add(19);
    
            ad1.pollFirst(); //deletes 6
            ad1.removeFirst(); //deletes 56
    
            System.out.println(ad1);
    
        }
    }
    

    Output

    [9, 10, 91, 19]

  • Removing the last element:

  • removeLast() & pollLast() methods are used to delete an element from the tail of the queue.
    Example :
    import java.util.*;
    public class CWH extends Thread{
        public static void main(String[] args) {
    
            ArrayDeque<Integer> ad1 = new ArrayDeque<>();
            ad1.add(6);
            ad1.add(56);
            ad1.add(9);
            ad1.add(10);
            ad1.add(91);
            ad1.add(19);
    
            ad1.pollLast(); //deletes 19
            ad1.removeLast(); //deletes 91
    
            System.out.println(ad1);
    
        }
    }
    

    Output

    [6, 56, 9, 10]

    Example


    package com.company;
    
    import java.util.ArrayDeque;
    
    public class arraydeque {
        public static void main(String[] args) {
            ArrayDeque<Integer> ad1 = new ArrayDeque<>();
            ad1.add(6);
            ad1.add(56);
            ad1.add(9);
            ad1.addFirst(5);
            System.out.println(ad1.getFirst());
            System.out.println(ad1.getLast());
        }
    }
    


    Conclusion

    In this page (written and validated by ) you learned about Java ArrayDeque . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java Hashing.



    Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


    Share On:


    Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.