Signup/Sign In

Queue Interface


In Java, the Queue interface is under java.util package. The queue extends the collection interface. It is used to hold an element in a collection which are to be processed and can also perform operations like insertion, deletion etc. In this interface elements are inserted at the end of the list and deleted from the start of the list. Its follows the concept of FIFO i.e First In First Out. To declare a queue a concrete class is required, mostly PriorityQueue and LinkedList class is used.


Below is the method of Queue interface.


S.no. Method Description
1 add() It is used for adding elements in the queue.
2 peek() It used to view the head of the queue..
3 element() It is used to check whether the queue is empty or not. If empty it throws NoSuchElementFound.
4 remove() It is used to remove the elements from the head of the queue.
5 poll() It is used to remove an element and return the head of the queue.
6 size() It is used to get the size of the element.

Example:

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

public class QueueDemo1 
{ 
  public static void main(String[] args) 
  { 
    Queue<Integer> a = new LinkedList<>(); 
    for (int i=0; i<10; i++) 
a.add(i); 
System.out.println("**************************************");
System.out.println("Elements of Queue : "+a); 
System.out.println("**************************************");
int b= a.remove(); 
System.out.println("Removed element from the Queue : " + b); 
System.out.println(a); 
System.out.println("**************************************");
int c = a.peek(); 
System.out.println("head of queue-" + c); 
System.out.println("**************************************");
int d= a.size(); 
System.out.println("Size of queue-" + d); 
  } 
}
	

queue-example