In this section we will learn what the Deque peek(), peekFirst(), and peekLast() methods are and how to use them in Java.
What is Java Deque peek() Method?
The Java Deque peek() method is used to return a copy of the first (head) element of a Deque object.
Note: this method does not remove the element but just returns a copy of that value.
Java peek() Method Syntax:
E peek()
peek() Method Parameters:
The method does not take an argument.
peek() Method Return Value:
The return value of this method is the first (head) element of the target Deque object.
Note: if the object is empty, the value null will return instead.
peek() Method Exceptions:
The method does not throw an exception.
Example: using Deque peek() method
import java.util.ArrayDeque; import java.util.Deque; class Main{ public static void main(String[] args) { Deque<Integer> deque = new ArrayDeque<>(); deque.add(30); deque.add(20); deque.add(10); deque.add(40); deque.add(50); System.out.println("The size of the Deque object before calling the peek method: "+deque.size()); System.out.println("Calling the method and here's the value it returned: "+deque.peek()); System.out.println("The size of the Deque object after calling the peek method: "+deque.size()); } }
Output:
The size of the Deque object before calling the peek method: 5 Calling the method and here's the value it returned: 30 The size of the Deque object after calling the peek method: 5
What is Java Deque peekFirst() Method?
The Java Deque peekFirst() method is used to return a copy of the first (head) element of a Deque object.
Note: this method does not remove the first element of the object.
Java peekFirst() Method Syntax:
E peekFirst()
peekFirst() Method Parameters:
The method does not take an argument.
peekFirst() Method Return Value:
The return value of this method is the first (head) element of the target Deque object.
Note: if the object is empty, the value null will return instead.
peekFirst() Method Exceptions:
The method does not throw an exception.
Example: using Deque peekFirst() method
import java.util.ArrayDeque; import java.util.Deque; class Main{ public static void main(String[] args) { Deque<Integer> deque = new ArrayDeque<>(); deque.add(30); deque.add(20); deque.add(10); deque.add(40); deque.add(50); System.out.println("The size of the Deque object before calling the peekFirst method: "+deque.size()); System.out.println("Calling the method and here's the value it returned: "+deque.peekFirst()); System.out.println("The size of the Deque object after calling the peekFirst method: "+deque.size()); } }
Output:
The size of the Deque object before calling the peekFirst method: 5 Calling the method and here's the value it returned: 30 The size of the Deque object after calling the peekFirst method: 5
What is Java Deque peekLast() Method?
The Java Deque peekLast() method is used to get a copy of the last (tail) element of a Deque object.
Note: this method does not remove this last element.
Java peekLast() Method Syntax:
E peekLast()
peekLast() Method Parameters:
The method does not take an argument.
peekLast() Method Return Value:
The return value of this method is a “copy” of the last (tail) element of the target Deque object.
Note: if the Deque object is empty, the value null will return as a result.
peekLast() Method Exceptions:
The method does not throw an exception.
Example: using Deque peekLast() method
import java.util.ArrayDeque; import java.util.Deque; class Main{ public static void main(String[] args) { Deque<Integer> deque = new ArrayDeque<>(); deque.add(30); deque.add(20); deque.add(10); deque.add(40); deque.add(50); System.out.println("The size of the Deque object before calling the peekLast method: "+deque.size()); System.out.println("Calling the method and here's the value it returned: "+deque.peekLast()); System.out.println("The size of the Deque object after calling the peekLast method: "+deque.size()); } }
Output:
The size of the Deque object before calling the peekLast method: 5 Calling the method and here's the value it returned: 50 The size of the Deque object after calling the peekLast method: 5