Wednesday 12 November 2014

Java : Collection Framework : Queue (Element and Peek methods)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=CZ6C3Vs_5JY&list=UUhwKlOVR041tngjerWxVccw

QueueExample.java
import java.util.LinkedList;
import java.util.Queue;

/*
 *  Example of element() and peek() methods.
 */
public class QueueExample
{

    public static void main( String[] args )
    {
        Queue<Integer> queue = new LinkedList<Integer>();
        queue.add(200);
        queue.add(300);
        queue.add(400);
        queue.add(500);

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

        /*
         * Retrieves, but does not remove, the head of this queue. This method
         * differs from peek only in that it throws an exception if this queue
         * is empty.
         */
        Integer element = queue.element();

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

        /*
         * Retrieves, but does not remove, the head of this queue, or returns
         * null if this queue is empty.
         */

        element = queue.peek();
        System.out.println("element : " + element);
        System.out.println("queue : " + queue + "\n");

    }
}

Output
queue : [200, 300, 400, 500]

element : 200
queue : [200, 300, 400, 500]

element : 200
queue : [200, 300, 400, 500]

To Download QueueDemoElementPeek Project Click the below link

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