Java ArrayBlockingQueue peek() Method
The peek() method of ArrayBlockingQueue returns the head element of the queue. It retrieves but does not remove the element. It returns null if the queue is empty.
Syntax:
public E peek();
Program to Retrieve Element using the Peek Method
Let’s have a look at how to use the peek() method to retrieve the head element from the queue.
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class ArrayBlockingQueuePeekExample { public static void main(String[] args) throws InterruptedException { int capacity = 5; BlockingQueuesalQue = new ArrayBlockingQueue<>(capacity); salQue.add(20000); salQue.add(28000); salQue.add(12000); salQue.add(19000); salQue.add(29000); System.out.println("Queue elements: \n" + salQue); System.out.println("Head of the queue is: " + salQue.peek()); } }
Output:
Queue elements: [20000, 28000, 12000, 19000, 29000] Head of the queue is: 20000
User-defined Object Example
The below example shows how to use the peek() method with the user-defined object.
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; class Employee { private int employeeId; private String employeeName; public Employee() { } public Employee(int employeeId, String employeeName) { super(); this.employeeId = employeeId; this.employeeName = employeeName; } @Override public String toString() { return "Employee [employeeId=" + employeeId + ", employeeName=" + employeeName + "]"; } } public class PeekDemo { public static void main(String[] args) { int capacity = 5; BlockingQueueemployeesQueue = new ArrayBlockingQueue<>(capacity); employeesQueue.add(new Employee(101, "John")); employeesQueue.add(new Employee(102, "Adam")); employeesQueue.add(new Employee(103, "Ricky")); employeesQueue.add(new Employee(104, "Shaun")); employeesQueue.add(new Employee(105, "Chris")); System.out.println("Queue elements: "); for (Employee employee : employeesQueue) { System.out.println(employee); } System.out.println("Head of the queue is: " + employeesQueue.peek()); } }
Output:
Queue elements: Employee [employeeId=101, employeeName=John] Employee [employeeId=102, employeeName=Adam] Employee [employeeId=103, employeeName=Ricky] Employee [employeeId=104, employeeName=Shaun] Employee [employeeId=105, employeeName=Chris] Head of the queue is: Employee [employeeId=101, employeeName=John]
Peek when ArrayBlockingQueue is Empty
If the queue is empty then peek returns null. Let’s have a look at the program.
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class PeekEmptyQueueDemo { public static void main(String[] args) throws InterruptedException { int capacity = 5; BlockingQueuenumbers = new ArrayBlockingQueue<>(capacity); numbers.add(25000); numbers.add(12000); numbers.add(45000); numbers.add(19000); numbers.add(70000); System.out.println("Queue elements: \n" + numbers); System.out.println("Head of the queue is: " + numbers.peek()); numbers.clear(); System.out.println("Queue after removing all elements: \n" + numbers); System.out.println("Head of the queue is: " + numbers.peek()); } }
Output:
Queue elements: [25000, 12000, 45000, 19000, 70000] Head of the queue is: 25000 Queue after removing all elements: [] Head of the queue is: null
That’s all about the peek() method of ArrayBlockingQueue. Please share if you find this article helpful.