In this shot, we will discuss how to use the Deque.getFirst()
method in Java.
The Deque.getFirst()
method is present in the Deque
interface inside the java.util
package.
Deque.getFirst()
is used to retrieve the first element of the deque. If the deque is empty, it throws a NoSuchElementException
.
Element getFirst()
The Deque.getFirst()
doesn’t take parameters.
The Deque.getFirst()
method returns the first element in the deque.
Let us take a look at the code snippet:
import java.util.*;class Main{public static void main(String[] args){Deque<Integer> d = new LinkedList<Integer>();d.add(212);d.add(621);d.add(23);d.add(1280);d.add(1171);System.out.println("Elements in the deque are: "+d);System.out.println("The head of the deque is: "+d.getFirst());}}
Main
class.main
function.Integer
type elements.Deque.add()
method.Deque.getFirst()
method and displayed the first element in the deque with a message.In this way, we can use Deque.getFirst()
method in Java.