Queue Interface In Java

In this article, we will study Queue Interface In Java.

The cue interface is available in the java.util.package and extends the click interface that is used to store items that are processed in first in first out (FIFO) order. It is an ordered list of objects, the use of which is limited to inserting elements at the end of the list. Removing elements at the beginning of the list (i.e. it follows the principle FIFO or first in, first out.


As an interface, the queue needs a specific class for declaration. The most common classes are PriorityQueue and LinkedList in Java. It should be noted that both implementations are not thread safe. PriorityBlockingQueue is an alternate implementation when a thread-safe implementation requires.

public interface Queue extends Collection 

Create queue objects

Since the queue is an interface, queue type objects cannot be created. We still need a class that extends this list to create an object. In addition, after the introduction of generics in Java 1.5, it is possible to restrict the type of object that can be stored in the queue. This secure type queue can define as follows:

Queue<Obj> queue = new PriorityQueue<Obj> (); 


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

public class QueueInterface {

	public static void main(String[] args)
		Queue<Integer> q
			= new LinkedList<>();

		// Adds elements {0, 10, 20, 30, 40} to the queue
		for (int i = 0; i < 5; i++)

		// Display contents of the queue.
		System.out.println("Elements of queue "
						+ q);

		// To remove the head of queue.
		int removedele = q.remove();
		System.out.println("removed element-"
						+ removedele);


		// To view the head of queue
		int head = q.peek();
		System.out.println("head of queue-"
						+ head);

		// Rest all methods of collection interface like size and contains can be used with this implementation.
		int size = q.size();
		System.out.println("Size of queue-"
						+ size);


Elements of queue [0, 10, 20, 30, 40]
removed element-0
[10, 20, 30, 40]
head of queue-1
Size of queue-4

That’s all, we will learn about Queue Interface In Java.

Next Article

1. PriorityQueue in Java
2. Stack Class in Java

Please share this post:
Posts created 67

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: