In this example, We will show you how to implement Poll(), Peek() and Size() Methods in java Deque Example.
Poll(), Peek() and Size() Methods In Deque Example
In this example, We will show you how to implement Poll(), Peek() and Size() Methods in java Deque Example. These methods description is as follows-
peek() - This method retrieves the head of the queue represented by this deque, but does not remove, or returns null if this deque is empty.
poll() - This method retrieves and removes the head of the queue represented by this deque or returns null if this deque is empty.
Size() - This method returns the number of elements in this deque.
Example:- DequeMethodsExample.javapackage devmanuals.com;
import java.util.*;
public class DequeMethodsExample {
public static void main(String[] args) {
Deque queue = new ArrayDeque();
queue.add("A");
queue.add("B");
queue.add("C");
queue.add("D");
queue.add("E");
System.out.println("Initial Size of Deque :" + queue.size());
// get value and does not remove element from Deque
System.out.println("Deque peek :" + queue.peek());
// get first value and remove that object from Deque
System.out.println("Deque poll :" + queue.poll());
System.out.println("Final Size of Deque :" + queue.size());
}
}
Output:-
|
Initial Size of Deque : 5
Deque peek : A Deque poll : A Final Size of Deque : 4 |
Download This Code

[ 0 ] Comments