In this section we will discuss how can peekLast() method be implemented in Deque inteface in java. This method retrieves the element at the tail position of the underlying deque but it does not delete that element, or returns 'null' if the underlying deque is empty.
peekLast() method of Deque Interface in Java.
In this section we will discuss how can peekLast() method be implemented in Deque inteface in java.
Syntax
E peekLast()
This method returns the last element of the underlying deque otherwise returns 'null' if deque is empty.
This method retrieves the element at the tail position of the underlying deque but it does not delete that element, or returns 'null' if the underlying deque is empty.
Parameter description
This method does not take any argument.
Example of peekLast() method
In this example we will show how does peekLast() method work in Deque interface. Through this example we will show how you can retrieve the element at the last 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 DequePeekLast { 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.peekLast(); System.out.println("Last element = " + obj); Iterator iq = dq.iterator(); while (iq.hasNext()) { dq.remove(); } System.out.println("Is Deque empty : " + dq.isEmpty()); // Here the implementation of peekLast() method will display null System.out.println("Therefore returns " +"'"+ dq.peekLast()+"'"); } }
Output :
Is Deque empty : false Elements of deque = [11, DEV, MANUALS] Size of deque : 3 Last element = MANUALS Is Deque empty : true Therefore returns 'null' |
[ 0 ] Comments