Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava Program to Implement ArrayDeque API

Java Program to Implement ArrayDeque API

The ArrayDeque in Java provides how to use resizable-array additionally to the implementation of the Deque interface. It is also referred to as Array Double Ended Queue or Array Deck. This is a special quite array that grows and allows users to feature or remove a component from each side of the queue.

java.lang.Object

    java.util.AbstractCollection<E>

        java.util.ArrayDeque<E>

All implemented interfaces of ArrayDeque in the hierarchy are:

  • Serializable, 
  • Cloneable, 
  • Iterable<E>, 
  • Collection<E>
  • Deque<E> 
  • Queue<E>

Array deques haven’t any capacity restrictions; they grow as necessary to support usage. They are not thread-safe; within the absence of external synchronization, they are doing not support concurrent access by multiple threads. Null elements are prohibited. This class is probably going to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue.

Implementation:

Java




// Java program to demonstrate implementation
// of ArrayDeque
  
import java.util.*;
public class ArrayDequeExample {
    public static void main(String[] args)
    {
        // Initializing an deque
        Deque<Integer> de_que = new ArrayDeque<Integer>(10);
  
        // add() method to insert
        de_que.add(10);
        de_que.add(20);
        de_que.add(30);
        de_que.add(40);
        de_que.add(50);
  
        for (Integer element : de_que) {
            System.out.println("Element : " + element);
        }
  
        System.out.println("Using clear() ");
  
        // clear() method
        de_que.clear();
  
        // addFirst() method to insert at start
        de_que.addFirst(564);
        de_que.addFirst(291);
  
        // addLast() method to insert at end
        de_que.addLast(24);
        de_que.addLast(14);
  
        System.out.println(
            "Above elements have been cleared");
  
        // Iterator() :
        System.out.println(
            "Elements of deque using Iterator :");
  
        for (Iterator itr = de_que.iterator();
             itr.hasNext();) {
            System.out.println(itr.next());
        }
  
        // descendingIterator() : to reverse the deque order
        System.out.println(
            "Print elements in reverse order :");
  
        for (Iterator dItr = de_que.descendingIterator();
             dItr.hasNext();)
        {
            System.out.println(dItr.next());
        }
  
        // element() method : to get Head element
        System.out.println(
            "\nHead Element using element(): "
            + de_que.element());
  
        // getFirst() method : to get Head element
        System.out.println(
            "Getting Head Element using getFirst(): "
            + de_que.getFirst());
  
        // getLast() method : to get last element
        System.out.println(
            "Getting Last Element using getLast(): "
            + de_que.getLast());
  
        // toArray() method :
        Object[] arr = de_que.toArray();
  
        System.out.println("\nArray Size : " + arr.length);
  
        System.out.print("Array elements : ");
  
        for (int i = 0; i < arr.length; i++)
            System.out.print(" " + arr[i]);
  
        // peek() method : to get head
        System.out.println("\nHead element : "
                           + de_que.peek());
  
        // poll() method : to get head
        System.out.println("Head element poll : "
                           + de_que.poll());
  
        // push() method :
        de_que.push(265);
        de_que.push(984);
        de_que.push(2365);
  
        // remove() method : to get head
        System.out.println("Head element remove : "
                           + de_que.remove());
  
        System.out.println("Final Array: " + de_que);
    }
}


Output

Element : 10
Element : 20
Element : 30
Element : 40
Element : 50
Using clear() 
Above elements have been cleared
Elements of deque using Iterator :
291
564
24
14
Print elements in reverse order :
14
24
564
291

Head Element using element(): 291
Getting Head Element using getFirst(): 291
Getting Last Element using getLast(): 14

Array Size : 4
Array elements :  291 564 24 14
Head element : 291
Head element poll : 291
Head element remove : 2365
Final Array: [984, 265, 564, 24, 14]
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments