< Prev
Next >



ArrayDeque Class





ArrayDeque is a collection class which extends AbstractCollection and implements Deque interface. As it implements Deque interface, which means a double ended queue, hence, ArrayDeque can be used to implement a first-in, first-out(FIFO) queue or a last-in, first-out(LIFO) queue.




Important features of ArrayDeque







A simple constructor of ArrayDeque


1) ArrayDeque()
This constructor creates an empty ArrayDeque to hold a particular object type.
Example -
ArrayDeque<Integer> arrL= new ArrayDequet<Integer>();
This constructor creates an ArrayDeque to hold Integer objects.




Some legacy methods in ArrayDeque class.


Methods Description
int size() Returns the total number of elements in ArrayDeque.
boolean isEmpty() Returns true if ArrayDeque is empty, else false.
void push(E element) Pushes the element on the top of ArrayDeque.
E pop() Returns and removes the element from the head of ArrayDeque.
E peek() Returns but doesn't remove element from the head of ArrayDeque.
E getFirst() Returns(doesn't remove) the first element from the head of ArrayDeque
E getLast() Returns(doesn't remove) the last/tail element in the ArrayDeque.





Implenting a stack(LIFO) using ArrayDeque


In this program we are creating a stack(LIFO) last-in, first-out(LIFO) structure using push() and pop() methods of ArrayDeque. As we know that in stack, elements are added and removed from the its top. Hence, method push() will insert an element at the top of the ArrayDeque, while method pop() will remove a top element from the ArrayDeque, making it behave exactly like a stack.

import java.util.*;

class ArrayDeque1
{
public static void main(String... ar)
{
ArrayDeque<Integer> st= new ArrayDeque<Integer>();
st.push(10);
st.push(23);
st.push(16);
st.push(5);
st.push(29); //29 is the last element to enter, hence, first one to exit this last-in, first-out(LIFO) ArrayDeque.
System.out.println("LIFO ArrayDeque = "+ st);

System.out.println("Popping out the top element = "+ st.pop());
System.out.println("Popping out the next top element = "+ st.pop());

System.out.println("LIFO ArrayDeque = "+ st);

System.out.println("First element = "+ st.getFirst());
System.out.println("Last element = "+st.getLast());

System.out.println("LIFO ArrayDeque = "+ st);
}
}


Output is :

LIFO ArrayDeque = [29, 5, 16, 23, 10]
Popping out the top element = 29
Popping out the next top element = 5
LIFO ArrayDeque = [16, 23, 10]
First element = 16
Last element = 10
LIFO ArrayDeque = [16, 23, 10]


Program Analysis







Let's implement a simple first-in, first out(FIFO)queue using ArrayDeque -:


In this program we are creating a a simple first-in, first-out(FIFO) queue using methods of ArrayDeque. We aren't going to use push(), which was used to make a Stack but we will rather use add() method to insert elements in order to create a first-in, first-out(FIFO) queue.

import java.util.*;

class ArrayDeque1
{
public static void main(String... ar)
{
ArrayDeque st= new ArrayDeque();
st.add(10); //10 is the first element to enter, hence, first one to exit this first-in, first-out(FIFO) ArrayDeque.
st.add(23);
st.add(16);
st.add(5);
st.add(29);
System.out.println("FIFO ArrayDeque = "+ st);

System.out.println("Popping out the top element = "+ st.pop());
System.out.println("Popping out the next top element = "+ st.pop());

System.out.println("FIFO ArrayDeque = "+ st);

System.out.println("First element = "+ st.getFirst());
System.out.println("Last element = "+st.getLast());

System.out.println("FIFO ArrayDeque = "+ st);
}
}


Output is :


FIFO ArrayDeque = [10, 23, 16, 5, 29]
Popping out the top element = 10
Popping out the next top element = 23
FIFO ArrayDeque = [16, 5, 29]
First element = 16
Last element = 29
FIFO ArrayDeque = [16, 5, 29]


Program Analysis






Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< PriorityQueue Class
For-Each to iterate Collection>
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2