E pollLast() This method deletes the last element from the deque.
pollLast() method of Deque Interface in Java.
In this section we will discuss how can pollLast() method be implemented in Deque interface in java.
Syntax
E pollLast()
This method deletes the last element from the deque.
In this method the last element of the underlying deque is retrieved and deleted. This method returns the element which is just removed at the last position from the deque, or returns 'null' if deque is empty.
Parameter description
This method has no any parameter.
Example of pollLast() method
In this example we will show you how does pollLast() method work in Deque interface in java. Through this example we are trying to show you how can the last element be removed from the underlying deque. In this example we used some more functions (that are properly described below) to clarify how the above mentioned method puts effect on deque.
Example :
package devmanuals.com; import java.util.Deque; import java.util.LinkedList; import java.util.Iterator; public class DequePollLast { public static void main(String[] args) { Deque dq = new LinkedList(); dq.add("Dev"); dq.add("Manuals"); dq.add(".Com"); dq.add("Java"); System.out.println("Is deque empty : " + dq.isEmpty()); System.out.println("Elements of deque : " + dq); System.out.println("Size of deque before removing an element : "+ dq.size()); System.out.println("Now after removing the last element from deque, new deque is "); Object obj = dq.pollLast(); System.out.println(dq); System.out.println("Removed element is = " + obj); System.out.println("Size of deque after removing element : "+ dq.size()); System.out.println("Removes all the elements from deque"); Iterator lq = dq.iterator(); while (lq.hasNext()) { dq.poll(); } System.out.println(dq); System.out.println("Is deque empty : " + dq.isEmpty()); // Here implementation of the pollLast( ) method will display 'null' System.out.println(dq.pollLast()); } }
Output :
Is deque empty : false Elements of deque : [Dev, Manuals, .Com, Java] Size of deque before removing an element : 4 Now after removing the last element from deque, new deque is [Dev, Manuals, .Com] Removed element is = Java Size of deque after removing element : 3 Removes all the elements from deque [] Is deque empty : true null |
[ 0 ] Comments