Poll(), Peek() and Size() Methods In Deque Example

Poll(), Peek() and Size() Methods In Deque Example


Posted in : Java Posted on : December 1, 2010 at 4:40 PM Comments : [ 0 ]

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.java
package 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
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics