Friday 14 November 2014

Java : Collection Framework : Deque (Get & Peek First Methods)

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

/*
 *  Example of getFirst() and peekFirst() 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, but does not remove, the first element of this deque. This
         * method differs from peekFirst only in that it throws an exception if
         * this deque is empty.
         */
        Integer element = deque.getFirst();

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

        /*
         * Retrieves, but does not remove, the first element of this deque, or
         * returns null if this deque is empty.
         */
        element = deque.peekFirst();
        System.out.println("element : " + element);
        System.out.println("deque : " + deque + "\n");

    }
}


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

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

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







To Download DequeDemoGetPeekFirst Click the below link

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