How to Instantiate a queue in JAVA ?

Last Updated On Wednesday 27th Oct 2021

how to instantiate a queue in java

A Queue is an interface, which means you cannot construct a Queue directly.

	Queue<Integer> q = new LinkedList<Integer>();


	Queue<Integer> q = new ArrayDeque<Integer>();

You can’t instantiate an interface directly except via an anonymous inner class.

	import java.util.*;
public class Main {
    public static void main(String[] args) {
        Queue queue = new LinkedList();
        // remove () =>removes first element from the queue
        queue.remove(); // 44 removed
        // element() => returns head of the queue
        queue.element(); // 45
        // peek() => returns the head of the queue without removing it.
        queue.peek(); // 45