The Wayback Machine - https://web.archive.org/web/20240618020143/https://www.geeksforgeeks.org/arraydeque-peek-method-in-java/
Open In App

ArrayDeque peek() Method in Java

Last Updated : 10 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The java.util.ArrayDeque.peek() method in Java is used to retrieve or fetch the element at the head of the Deque. The element retrieved does not get deleted or removed from the Queue instead the method just returns it. If no element is present in the deque then Null is returned.

Syntax:

Array_Deque.peek()

Parameters: The method does not take any parameter.

Return Value: The method returns the element at the head of the Deque.

Below programs illustrate the Java.util.ArrayDeque.peek() method:
Program 1:




// Java code to illustrate peek()
import java.util.*;
  
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        ArrayDeque<String> de_que = new ArrayDeque<String>();
  
        // Use add() method to add elements into the Deque
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
  
        // Displaying the ArrayDeque
        System.out.println("Initial ArrayDeque: " + de_que);
  
        // Displaying the head
        System.out.println("The element at head is: " + 
                                           de_que.peek());
  
        // Displaying the ArrayDeque after operation
        System.out.println("Final ArrayDeque: " + de_que);
    }
}


Output:

Initial ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
The element at head is: Welcome
Final ArrayDeque: [Welcome, To, Geeks, 4, Geeks]

Program 2:




// Java code to illustrate peek()
import java.util.*;
  
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        ArrayDeque<Integer> de_que = new ArrayDeque<Integer>();
  
        // Use add() method to add elements into the Deque
        de_que.add(10);
        de_que.add(15);
        de_que.add(30);
        de_que.add(20);
        de_que.add(5);
  
        // Displaying the ArrayDeque
        System.out.println("Initial ArrayDeque: " + de_que);
  
        // Displaying the head
        System.out.println("The element at head is: " + 
                                           de_que.peek());
  
        // Displaying the ArrayDeque after operation
        System.out.println("Final ArrayDeque: " + de_que);
    }
}


Output:

Initial ArrayDeque: [10, 15, 30, 20, 5]
The element at head is: 10
Final ArrayDeque: [10, 15, 30, 20, 5]

Program 3: For an empty deque:




// Java code to illustrate peek()
import java.util.*;
  
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        ArrayDeque<Integer> de_que = new ArrayDeque<Integer>();
  
        // Displaying the ArrayDeque
        System.out.println("ArrayDeque: " + de_que);
  
        // Displaying the head
        System.out.println("The element at head is: " + de_que.peek());
    }
}


Output:

ArrayDeque: []
The element at head is: null


Previous Article
Next Article

Similar Reads

Java 8 | ArrayDeque removeIf() method in Java with Examples
The removeIf() method of ArrayDeque is used to remove all those elements from ArrayDeque which satisfies a given predicate filter condition passed as a parameter to the method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition che
3 min read
Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
Linked list class offers the functionality to "look into" the first and last elements of the list and hence can be useful in cases where only the retrieval is required and not necessarily the deletion is required. Three functionalities are present and all are discussed in this article. 1. peek() : This method retrieves, but does not remove, the hea
4 min read
ArrayBlockingQueue peek() Method in Java
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework.Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.The queue also follows FIFO (first-in-first-out) rule for stor
3 min read
PriorityQueue peek() Method in Java
The java.util.PriorityQueue.peek() method in Java is used to retrieve or fetch the first element of the Queue or the element present at the head of the Queue. The element retrieved does not get deleted or removed from the Queue. Syntax: Priority_Queue.peek() Parameters: The method does not take any parameters. Return Value: The method returns the e
2 min read
Stack peek() Method in Java
The java.util.Stack.peek() method in Java is used to retrieve or fetch the first element of the Stack or the element present at the top of the Stack. The element retrieved does not get deleted or removed from the Stack. Syntax: STACK.peek() Parameters: The method does not take any parameters. Return Value: The method returns the element at the top
2 min read
LinkedBlockingQueue peek() method in Java
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 illus
3 min read
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 Priority
2 min read
LinkedBlockingDeque peek() method in Java
The peek() method of LinkedBlockingDeque returns the front element in the Deque container. It returns null if the container is empty. Syntax: public E peek() Parameters: This method does not accept any parameters. Returns: This method returns front element in the Deque container if the container is not empty. It returns false if the container is em
2 min read
LinkedTransferQueue peek() method in Java
The java.util.concurrent.LinkedTransferQueue.peek() method is an in-built function in Java which is used to return the head of the queue if the queue is non-empty. Syntax: LinkedTransferQueue.peek() Parameters: The function does not accept any parameter. Return Value: The function returns the head of the queue if the queue is non-empty, otherwise i
2 min read
ConcurrentLinkedQueue peek() method in Java
The peek() method of ConcurrentLinkedQueue is used to return the head of the ConcurrentLinkedQueue. It retrieves but does not remove, the head of this ConcurrentLinkedQueue. If the ConcurrentLinkedQueue is empty then this method returns null. Syntax: public E peek() Returns: This method returns the head of this ConcurrentLinkedQueue without removin
2 min read