Java poll method, Example PriorityQueue Poll() method.

Java poll method, Example PriorityQueue Poll() method.


Posted in : Core Java Posted on : December 9, 2010 at 6:40 PM Comments : [ 0 ]

Java poll method, Example PriorityQueue Poll() method.

Java poll method, Example PriorityQueue Poll() method.

In this example, you will see the use of poll() method of java PriorityQueue class. It retrieves the head  elements of this queue, and remove it from queue. If queue is empty than it returns null.

Code:

PriorityQueuePollMethod.java

import java.util.Iterator;
import java.util.PriorityQueue;

public class PriorityQueuePollMethod {
public static void main(String[] args) {
	PriorityQueue obQueue = new PriorityQueue(5);
	obQueue.add(1);
	obQueue.add(2);
	obQueue.add(3);
	obQueue.add(4);
	System.out
	.println("Before poll method elements in priority queue : ");
	Iterator obIterator = obQueue.iterator();
	while (obIterator.hasNext()) {
		System.out.print(obIterator.next() + " ");
	}
	System.out.println("\nHead element :");
	System.out.print(obQueue.poll());
	System.out.println("\nAfter poll method elements in priority queue : ");
	Iterator obIterator1 = obQueue.iterator();
	while (obIterator1.hasNext()) {
		System.out.print(obIterator1.next() + " ");
	}
    }
}
Output:
Before poll method elements in priority queue :
1 2 3 4
Head element :
1
After poll method elements in priority queue :
2 4 3

Download this code.
Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics