The java.util.PriorityQueue.peek() method in Java is used to retrieve or fetch the first element of the Queue or the element present at the head of the Queue. The element retrieved does not get deleted or removed from the Queue.
Parameters: The method does not take any parameters.
Return Value: The method returns the element at the head of the Queue else returns NULL if the Queue is empty.
Below programs illustrate the java.util.PriorityQueue.peek() method:
Initial PriorityQueue: [For, Geeks, To, Welcome, Geeks] The element at the head of the queue is: For Final PriorityQueue: [For, Geeks, To, Welcome, Geeks]
Initial PriorityQueue: [5, 10, 30, 20, 15] The element at the head of the queue is: 5 Final PriorityQueue: [5, 10, 30, 20, 15]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.