Thursday 13 November 2014

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


Click here to watch in Youtube :
https://www.youtube.com/watch?v=Op-7j3dAQCI&list=UUhwKlOVR041tngjerWxVccw

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

/*
 *  Example of removeFirst() and pollFirst() 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);

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

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

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

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

    }
}


Output
deque : [100, 200, 300]

removedElement : 100
deque : [200, 300]

removedElement : 200
deque : [300]



To Download DequeDemoRemovePollFirst Project Click the below link

https://sites.google.com/site/javaee4321/java-collections/DequeDemoRemovePollFirst.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