PriorityBlockingQueue peek() method in Java
The peek() method of PriorityBlockingQueue returns the element at head of the PriorityBlockingQueue. It retrieves the value of the head of LinkedBlockingQueue but does not remove it. If the PriorityBlockingQueue does not contains any element, then this method returns null. A PriorityBlockingQueue queue uses the same ordering rules as class PriorityQueue.
public E peek()
Parameter: This method does not take any parameter.
Returns: This method returns the head of the PriorityBlockingQueue.
Below program illustrate peek() method of PriorityBlockingQueue.
PrioQueue: [416165, 464161] Head of Queue: 416165
Example 2: To illustrate peek() method of PriorityBlockingQueue which contains list of names.
List of Names: [A, Computer, Geeks, forGeeks, Portal] Head of Queue: A First Name from head is removed List of Names: [Computer, Portal, Geeks, forGeeks] Head of Queue: Computer
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.