Advertisement



< Prev
Next >



ArrayDeque Class





ArrayDeque is a collection class which extends the AbstractCollection class and also implements the Deque interface. As it implements the Deque interface, which means a double ended queue, hence, the ArrayDeque class 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.



Advertisement




Implementing a stack(LIFO) by using ArrayDeque


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

//Java - Implementing a stack(LIFO) by using ArrayDeque


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







Implementing a queue(FIFO) by using ArrayDeque


In this program we are creating a queue(FIFO) i.e. a first-in, first-out(FIFO) structure by using the methods of ArrayDeque. We aren't going to call the push() method of ArrayDeque, which was used to create a Stack, but we will rather call the add() method for the inserting of elements to create a first-in, first-out(FIFO) queue.

//Java - Implementing a queue(FIFO) by using ArrayDeque


import java.util.*;

class ArrayDeque1
{
public static void main(String... ar)
{
ArrayDeque<Integer> st= new ArrayDeque<Integer>();
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 -




< Prev
Next >
< PriorityQueue Class
For-Each to iterate Collection>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement