Home | FAQ | Contact me

Java Queues

Quick and dirty demonstration of Java Queue class behavior.

Notes:

If the case of the queue being empty is "usual" or "normal," likely it's best to use peek() and poll(). Otherwise, if the queue should never be empty, choose the other two.

package com.etretatlogiciels.queue;

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

public class DemoQueue
{
  static void println( String string )
  {
    if( string == null )
      System.out.println();
    else
      System.out.println( string );
  }

  public static void main( String[] args )
  {
    System.out.println( "Queue in Java" );
    System.out.println( "-----------------------" );

    println( "  You cannot create an instance of Queue as it's abstract," );
    println( "  so you create an instance of LinkedList and assign it to" );
    println( "  a Queue object." );
    Queue< String > queue = new LinkedList< String >();

    println( null );
    println( "  Add elements to queue..." );
    queue.add( "Java" );
    queue.add( "Python" );

    queue.add( "JavaScript" );
    queue.add( "HTML 5" );
    queue.add( "Hadoop" );

    println( null );
    println( "  Items in the queue: " + queue );
    println( null );

    println( "  Remove the first element from the queue, here Java:" );
    println( "               +------+" );
    println( "    remove():  | " + queue.remove() + " |" );
    println( "               +------+" );

    println( "  Retrieve the next/topmost element in the queue, as Java is gone, Python:" );
    println( "               +--------+" );
    println( "    element(): | " + queue.element() + " |" );
    println( "               +--------+" );

    println( "  Retrieve AND remove the next/topmost element of the queue, still Python:" );
    println( "               +--------+" );
    println( "    poll():    | " + queue.poll() + " |" );
    println( "               +--------+" );

    println( "  Peek at the current, topmost element in the queue to peek at it, JavaScript:" );
    println( "               +------------+" );
    println( "    peek():    | " + queue.peek() + " |" );
    println( "               +------------+" );
  }
}

Output:

Queue in Java
-----------------------
  You cannot create an instance of Queue as it's abstract,
  so you create an instance of LinkedList and assign it to
  a Queue object.

  Add elements to queue...

  Items in the queue: [Java, Python, JavaScript, HTML 5, Hadoop]

  Remove the first element from the queue, here Java:
               +------+
    remove():  | Java |
               +------+
  Retrieve the next/topmost element in the queue, as Java is gone, Python:
               +--------+
    element(): | Python |
               +--------+
  Retrieve AND remove the next/topmost element of the queue, still Python:
               +--------+
    poll():    | Python |
               +--------+
  Peek at the current, topmost element in the queue to peek at it, JavaScript:
               +------------+
    peek():    | JavaScript |
               +------------+