Queue interface in java

Queue interface:

Queue represents a collection for holding elements prior to processing.
Note: Queue declares its own methods in addition with methods declared in Collection interface.

Commonly used methods of Queue Interface:

1. add(Object obj): Inserts the specified element into this queue. Returns true if queue changed after the operation otherwise returns false.

Syntax: public boolean add(Object obj)

2. offer(Object obj): Inserts the specified element into this queue. Returns true if the element was added to this queue otherwise returns false.

Syntax: public boolean offerr(Object obj)

3. remove(): Returns and removes the head of this queue. It throws an exception if this queue is empty.

Syntax: public Object remove()

4. poll(): Returns and removes the head of this queue. Returns null if this queue is empty.

Syntax: public Object poll()

5. element(): Returns but does not removes the head of this queue. It throws an exception if this queue is empty.

Syntax: public Object element()

6. peek(): Returns but does not removes the head of this queue. Returns null if this queue is empty.

Syntax: public Object peek()

A simple example of PriorityQueue class to explain few methods of Queue interface.

 PriorityQueueTest.java

import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;
 
/**
 * This class is used to show the PriorityQueue functionality.
 * @author w3schools
 */
public class PriorityQueueTest {
	public static void main(String args[]){
		//Create PriorityQueue  object.
		Queue priorityQueue  = new PriorityQueue();
 
		//Add objects to the PriorityQueue .
		priorityQueue.add("Gourav");
		priorityQueue.add("Neeraj");
		priorityQueue.add("Deepak");
		priorityQueue.add("Mohan");
		priorityQueue.add("Parmender");
 
		//Print the PriorityQueue object.
		System.out.println("HasPriorityQueue elements:");
		System.out.println(priorityQueue);
 
		//Print the PriorityQueue elements using iterator.
		Iterator iterator1=priorityQueue.iterator();
		System.out.println("PriorityQueue elements " +
				"using iterator:");
		while(iterator1.hasNext()){  
		   System.out.println(iterator1.next());  
		}
 
		//Print the head element of the PriorityQueue
		System.out.println("Head element: " 
				+ priorityQueue.element());
		System.out.println("Head element: " 
				+ priorityQueue.peek());
 
		//Remove the head element of the PriorityQueue
		priorityQueue.poll();
		priorityQueue.remove();
 
		//Print the PriorityQueue object.
		System.out.println("HasPriorityQueue elements " +
				"after manipulation:");
		System.out.println(priorityQueue);
 
		//Print the PriorityQueue elements using iterator.
		Iterator iterator2=priorityQueue.iterator();
		System.out.println("PriorityQueue elements after " +
				"manipulation using iterator:");
		while(iterator2.hasNext()){  
		   System.out.println(iterator2.next());  
		}
	}
}

Output:

HasPriorityQueue elements:
[Deepak, Mohan, Gourav, Neeraj, Parmender]
PriorityQueue elements using iterator:
Deepak
Mohan
Gourav
Neeraj
Parmender
Head element: Deepak
Head element: Deepak
HasPriorityQueue elements after manipulation:
[Mohan, Neeraj, Parmender]
PriorityQueue elements after manipulation using iterator:
Mohan
Neeraj
Parmender

Download this example.
 
Next Topic: Deque interface in java with example.
Previous Topic: SortedMap interface in java with example.