The
peek() method of LinkedBlockingQueue returns the head of the LinkedBlockingQueue. It retrieves the value of the head of LinkedBlockingQueue but does not remove it. If the LinkedBlockingQueue is empty then this method returns null.
Syntax:
public E peek()
Return Value: This method returns the head of the LinkedBlockingQueue.
Below programs illustrates peek() method of LinkedBlockingQueue class:
Program 1: Returning the head of the LinkedBlockingQueue using peek() method where LinkedBlockingQueue of capacity 7 contains list of names
Output:
Queue is [John, Tom, Clark, Kat]
Head of Queue is John
Removing one element from Queue
Queue is [Tom, Clark, Kat]
Head of Queue is Tom
Program 2: Finding head of LinkedBlockingQueue which contains list of Employees