Java peek method, Example PriorityQueue peek() method.
Java peek method, Example PriorityQueue peek() method.
In this example, you will see the use of peek() method of java PriorityQueue class. It retrieves the head of this queue, but does not remove it. If queue is empty than it returns null.
Code:
SizeOfPriorityQueue.java
import java.util.Iterator; import java.util.PriorityQueue; public class PriorityQueuePeekMethod { public static void main(String[] args) { PriorityQueue obPriorQueue = new PriorityQueue(); obPriorQueue.add("B"); obPriorQueue.add("A"); obPriorQueue.add("C"); obPriorQueue.add("D"); obPriorQueue.add("E"); System.out .println("Before using peek method elements in priority queue : "); Iterator oIterator = obPriorQueue.iterator(); while (oIterator.hasNext()) { System.out.print(oIterator.next() + " "); } System.out.println("\n Head element : "); System.out.print(obPriorQueue.peek()); System.out.println("\nAfter peek method elements in priority queue : "); Iterator obIterator = obPriorQueue.iterator(); while (obIterator.hasNext()) { System.out.print(obIterator.next() + " "); } } }Output:
Before using peek method elements in priority queue : A B C D E Head element : A After peek method elements in priority queue : A B C D E |
Download this code.
[ 0 ] Comments