ArrayDeque

ArrayDeque in Java is a double-ended queue implementation that provides more efficient insertion and removal operations compared to LinkedList. It does not have capacity restrictions like arrays and allows elements to be added or removed from both ends of the deque. This makes it suitable for implementing stacks, queues, and dequeues efficiently.


import java.util.ArrayDeque;

// Syntax for creating an ArrayDeque
ArrayDeque<Type> dequeName = new ArrayDeque<>();Code language: JavaScript (javascript)

Methods:

  1. Adding Elements:
    • boolean add(E e): Adds the specified element to this set if it is not already present (optional operation).
  2. Removing Elements:
    • boolean remove(Object o): Removes the specified element from this set if it is present (optional operation).
    • void clear(): Removes all of the elements from this set (optional operation).
  3. Querying Set Information:
    • int size(): Returns the number of elements in this set.
    • boolean isEmpty(): Returns true if this set contains no elements.
  4. Checking Membership:
    • boolean contains(Object o): Returns true if this set contains the specified element.
  5. Bulk Operations:
    • boolean containsAll(Collection<?> c): Returns true if this set contains all of the elements of the specified collection.
    • boolean removeAll(Collection<?> c): Removes from this set all of its elements that are contained in the specified collection (optional operation).
    • boolean retainAll(Collection<?> c): Retains only the elements in this set that are contained in the specified collection (optional operation).
  6. Conversion to Array:
    • Object[] toArray(): Returns an array containing all of the elements in this set.
  7. Iteration:
    • Iterator<E> iterator(): Returns an iterator over the elements in this set.
import java.util.ArrayDeque;

public class ArrayDequeExample {
    public static void main(String[] args) {
        // Creating an ArrayDeque to store student names
        ArrayDeque<String> studentDeque = new ArrayDeque<>();

        // Adding students to the deque
        studentDeque.addLast("Paani");
        studentDeque.addLast("Mahesh");
        studentDeque.addLast("Datta");
        studentDeque.addLast("Ganesh");
        studentDeque.addLast("Harsha");

        // Displaying the deque of students
        System.out.println("Deque of students:");
        System.out.println(studentDeque);

        // Removing a student from the beginning of the deque
        String removedStudent = studentDeque.removeFirst();
        System.out.println("\nRemoved student from the beginning: " + removedStudent);

        // Displaying the updated deque
        System.out.println("\nUpdated deque of students:");
        System.out.println(studentDeque);

        // Adding a student at the beginning of the deque
        studentDeque.addFirst("Shyam");
        System.out.println("\nAdded student at the beginning: Shyam");

        // Displaying the updated deque
        System.out.println("\nUpdated deque of students:");
        System.out.println(studentDeque);

        // Checking the first and last students in the deque
        String firstStudent = studentDeque.getFirst();
        String lastStudent = studentDeque.getLast();
        System.out.println("\nFirst student in the deque: " + firstStudent);
        System.out.println("Last student in the deque: " + lastStudent);

        // Size of the deque
        System.out.println("\nSize of the deque: " + studentDeque.size());

        // Clearing the deque
        studentDeque.clear();
        System.out.println("\nIs the deque empty now? " + studentDeque.isEmpty());
    }
}

D:\>javac HashtableExample.java

D:\>java HashtableExample
Hashtable of students:
{Ganesh=104, Paani=101, Mahesh=102, Harsha=105, Datta=103}

Removed student 'Datta' with roll number: 103

Updated Hashtable of students:
{Ganesh=104, Paani=101, Mahesh=102, Harsha=105}

Does the Hashtable contain student 'Harsha'? true

Iterating over Hashtable entries using enumeration:
Student: Ganesh, Roll Number: 104
Student: Paani, Roll Number: 101
Student: Mahesh, Roll Number: 102
Student: Harsha, Roll Number: 105

Size of the Hashtable: 4

Is the Hashtable empty now? trueCode language: JavaScript (javascript)

ArrayDeque in Java is a versatile data structure that offers efficient insertion, removal, and access operations from both ends of the deque. It is particularly useful when implementing double-ended queues, stacks, or queues where elements need to be added or removed efficiently from both the front and the back. Understanding how to utilize ArrayDeque enables developers to design efficient and flexible data structures in Java applications.

Scroll to Top