Deque in Java is an interface present in java.util package. The Deque interface in Java was added by Java 6 version. It extends the Queue interface and declares the behavior of a double-ended-queue. In Deque we can add or remove elements from both sides of the queue. The Deque can function as standard, first-in, first-out queues or as last-in, first-out stacks. The Deque doesn't allow you to insert the null element. LinkedList and ArrayDeque are the two implementation class of the Deque interface.
Output:
You may also like these related posts:
How to implement PriorityQueue in Java?
Comparable vs Comparator in Java with an example
TreeSet in Java with Example
NavigableSet in Java with Example
How to implement a stack in java?
Difference between ArrayList and Vector
How to add and remove elements in the ArrayList?
Methods of Java Deque interface:
1. add(E e): This method is used to insert specified element to the tail.
2. addFirst(E e): This method is used to insert specified element to the head.
3. addLast(E e): This method is used to insert the specified element to the tail.
4. E getFirst(): This method returns the first element in the deque.
5. E getLast(): This method returns the last element in the deque.
6. offer(E e): This method adds an element to the tail of the deque and returns a boolean value.
7. offerFirst(E e): This method adds an element to the head of the queue and returns a boolean value if the insertion was successful.
8. offerLast(E e): This method adds an element to the tail of the queue and returns a boolean value if the insertion was successful.
9. removeFirst(): It removes the element at the head of the deque.
10. removeLast(): It removes the element at the tail of the deque.
11. push(E e): This method adds the specified element at the head of the queue
12. pop(): It removes the element from the head and returns it.
13. poll(): returns and remove the head element of the deque.
14. pollFirst(): returns and remove the first element of the deque. It returns null if the deque is empty.
15. pollLast(): returns and remove the last element of the deque. It returns null if the deque is empty.
16. peek(): return the element at the head of a deque but not removed. It returns null if the deque is empty.
17. peekFirst(): return the first element at the head of a deque but not removed. It returns null if the deque is empty.
18. peekLast(): return the last element at the head of a deque but not removed. It returns null if the deque is empty.
Java Deque Example
import java.util.*;
class DequeExample{
public static void main(String args[]){
Deque<String> dq = new LinkedList<String>();
//adding the element in the deque
dq.add("Ajay");
dq.add("Vijay");
dq.add("Rahul");
dq.addFirst("Amit");
dq.addLast("Sumit");
System.out.println("Deque elements are: " +dq);
//remove last element
System.out.println("remove last: " +dq.removeLast());
/*return the element at the head of a deque but not removed.
It returns null if the deque is empty.*/
System.out.println("peek(): " +dq.peek());
//returns and remove the head element of the deque.
System.out.println("poll(): " +dq.poll());
/* returns and remove the first element of the deque.
It returns null if the deque is empty.*/
System.out.println("pollFirst(): " +dq.pollFirst());
//dispalying deque element
System.out.println("After all operation deque elements are: " +dq);
}
}
Output:
You may also like these related posts:
How to implement PriorityQueue in Java?
Comparable vs Comparator in Java with an example
TreeSet in Java with Example
NavigableSet in Java with Example
How to implement a stack in java?
Difference between ArrayList and Vector
How to add and remove elements in the ArrayList?
DeQue in Java with Example
Reviewed by Prashant Srivastava
on
March 16, 2019
Rating:
No comments: