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>();
	

or

	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();
        queue.add(44);
        queue.add(45);
        queue.add(46);
        // 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
    }
}