Monday 17 November 2014

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


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

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

/*
 *  Example of remove(),poll() methods.
 */
public class DequeExample
{

    public static void main(String[] args)
    {
        Deque<Integer> deque = new LinkedList<Integer>();
        deque.add(2000);
        deque.add(5000);
        deque.add(10000);
        deque.add(9000);

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

        /*
         * Retrieves and removes the head of the queue represented by this deque
         * (in other words, the first element of this deque). This method
         * differs from poll only in that it throws an exception if this deque
         * is empty.
         */
        Integer removedElement = deque.remove();
        System.out.println("removedElement : " + removedElement);
        System.out.println("deque : " + deque + "\n");

        /*
         * Retrieves and removes the head of the queue represented by this deque
         * (in other words, the first element of this deque), or returns null if
         * this deque is empty.
         */
        removedElement = deque.poll();
        System.out.println("removedElement : " + removedElement);
        System.out.println("deque : " + deque + "\n");

    }

}





Output
deque : [2000, 5000, 10000, 9000]

removedElement : 2000
deque : [5000, 10000, 9000]

removedElement : 5000
deque : [10000, 9000]

To Download DequeDemoRemovePoll Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/DequeDemoRemovePoll.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