Using a Queue in Java's library

  • Unlike the Stack (which is a class in Java), the Queue is an interface:

  • The Queue interface is implemented by several class, including:

    • ArrayDeque
    • PriorityQueue

Example using a Queue in Java's library

  • You must read the description of the Queue interface definition and find out the methods you need to use :

    • add(E e): Inserts the specified element into this queue

    • remove(): Retrieves and removes the head of this queue.


  • Then pick a class that implements the Queue interface

    Example:

        ArrayDeque<E>   (it's a generic class)
    

  • Finally, read the description of the ArrayQueue class definition and find out how to create a ArrayDeque<E> object :

     Constructor:  ArrayDeque() (Remember: it's a generic class !)
    

You can now write a Java program that uses a Queue

Example using a Queue in Java's library

    public static void main(String[] args)
    {
        Queue<Integer> s = new ArrayDeque<>();

        s.add(1);
        System.out.println( s );
        s.add(2);
        System.out.println( s );

	System.out.println( s.remove() );
        System.out.println( s );
    }

DEMO: 10-queue/02-java-queue/Demo.java