Thursday, 13 November 2014

Java : Collection Framework : Deque (Remove & Poll Last Methods)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=ELIqGs6U-0s&list=UUhwKlOVR041tngjerWxVccw

DequeExample.java
import java.util.Deque;
import java.util.LinkedList;

/*
 *  Example of removeLast() and pollLast() methods.
 */
public class DequeExample
{

    public static void main( String[] args )
    {
        Deque<Integer> deque = new LinkedList<Integer>();
        deque.add(100);
        deque.add(200);
        deque.add(300);
        deque.add(400);

        System.out.println("deque : " + deque + "\n");

        /*
         * Retrieves and removes the last element of this deque. This method
         * differs from pollLast only in that it throws an exception if this
         * deque is empty.
         */
        Integer removedElement = deque.removeLast();

        System.out.println("removedElement : " + removedElement);
        System.out.println("deque : " + deque + "\n");

        /*
         * Retrieves and removes the last element of this deque, or returns null
         * if this deque is empty.
         */
        removedElement = deque.pollLast();
        System.out.println("removedElement : " + removedElement);
        System.out.println("deque : " + deque + "\n");

    }
}



Output
deque : [100, 200, 300, 400]

removedElement : 400
deque : [100, 200, 300]

removedElement : 300
deque : [100, 200]




To Download DequeDemoRemovePollLast Project Click the below link

https://sites.google.com/site/javaee4321/java-collections/DequeDemoRemovePollLast.zip?attredirects=0&d=1

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment