The
peek() method of
Queue Interface returns the element at the front the container. It does not deletes the element in the container. This method returns the head of the queue. The method does not throws an exception when the Queue is empty, it returns
null instead.
Syntax:
E peek()
Returns: This method returns the
head of the Queue, it returns false when the Queue is empty
Below programs illustrate peek() method of Queue:
Program 1: With the help of
LinkedList.
Output:
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue: [7855642, 35658786, 5278367, 74381793]
Program 2: To demonstrate peek() method of Queue when Queue is empty
Output:
Queue: []
Queue's head: null
Program 3: With the help of
ArrayDeque.
Output:
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Program 4: With the help of
LinkedBlockingDeque.
Output:
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Program 5: With the help of
ConcurrentLinkedDeque.