VOOZH about

URL: https://www.geeksforgeeks.org/java/linkedblockingqueue-peek-method-in-java/

⇱ LinkedBlockingQueue peek() method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

LinkedBlockingQueue peek() method in Java

Last Updated : 26 Nov, 2018
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
Output:
Head of list
Employee Name : Ravi
Employee Position : Tester
Employee Salary : 39000

Removing one element from Queue

Head of list
Employee Name : Sanjeet
Employee Position : Manager
Employee Salary : 98000
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#peek--
Comment