PriorityQueue class peek()method example
Retrieves, but does not remove, the head of this queue
PriorityQueue class peek()method example. This example shows you how to use peek()method.This method Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
Here is the code:-
>
/**
* @Program that Retrieves the head of this queue,and returns null if this
* queue is empty.
* Peek.java
* Author:-RoseIndia Team
* Date:-28-May-2008
*/
import java.util.*;
public class Peek {
public static void main(String[] args) {
//Creating a PriorityQueue
PriorityQueue p = new PriorityQueue();
//Adding element to the queue
p.add("1");
p.add("2");
p.add("3");
p.add("4");
p.add("5");
p.add("6");
Iterator i = p.iterator();
while (i.hasNext()) {
System.out.print(i.next() + "\t");
}
System.out.println();
//Retrieves the head of this queue
System.out.print("The Head of the Queue is: " + p.peek());
}
} |
Output of the program:-
1 2 3 4 5 6
The Head of the Queue is: 1 |
|