Java ArrayDeque - peekFirst() Method
The java.util.ArrayDeque.peekFirst() method is used to retrieve the head (first element) of the deque. The method returns null, if the deque is empty. Unlike the ArrayDeque pollFirst method, it does not remove the retrieved element.
Syntax
public E peekFirst()
Here, E is the type of element maintained by the container.
Parameters
No parameter is required.
Return Value
Returns the head (first element) of the deque, or null if the deque is empty.
Exception
NA.
Example:
In the example below, the java.util.ArrayDeque.peekFirst() method is used to retrieve the head of the given deque.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a ArrayDeque ArrayDeque<Integer> MyDeque = new ArrayDeque<Integer>(); //populating ArrayDeque MyDeque.add(10); MyDeque.add(20); MyDeque.add(30); //printing ArrayDeque System.out.println("MyDeque contains: "); while(MyDeque.size() != 0) { System.out.println(MyDeque.peekFirst()); MyDeque.removeFirst(); } } }
The output of the above code will be:
MyDeque contains: 10 20 30
❮ Java.util - ArrayDeque