E peekFirst()� This method returns the first element of the underlying deque.
peekFirst() method of Deque Interface in Java.
In this section we will discuss how can peekFirst() method be implemented in Deque interface in java.
Syntax
E peekFirst()
This method returns the first element of the underlying deque.
This method is equivalent to the peek() method as this method returns the element at the head position of the deque, the peekFirst() method also do work exactly like the peek() method does. This method retrieves the first element of the underlying deque but it does not delete that element, or returns 'null' if the underlying deque is vacate.
Parameter description
This method does not take any argument.
Example of peekFirst( ) method
In this example we will show how does peekFirst() method work in Deque interface. Through this example we will show how you can retrieve the element at the head position of the underlying deque, and count the total number of elements in deque.
Example :
package devmanuals.com; import java.util.Deque; import java.util.LinkedList; import java.util.Iterator; public class DequePeekFirst { public static void main(String args[]) { Deque<String> dq = new LinkedList<String>(); dq.add("11"); dq.add("DEV"); dq.add("MANUALS"); System.out.println("Is Deque empty : " + dq.isEmpty()); System.out.println("Elements of deque = " + dq); System.out.println("Size of deque : " + dq.size()); Object obj = dq.peekFirst(); System.out.println("Head element = " + obj); Iterator iq = dq.iterator(); while (iq.hasNext()) { dq.remove(); } System.out.println("Is Deque empty : " + dq.isEmpty()); // Here the implementation of peekFirst() method will display null System.out.println("Therefore returns "+"'"+ dq.peekFirst()+"'"); } }
Output :
Is Deque empty : false Elements of deque = [11, DEV, MANUALS] Size of deque : 3 Head element = 11 Is Deque empty : true Therefore returns 'null' |
[ 0 ] Comments