AbstractQueue Class in Java

The AbstractQueue class belongs to the Java Collection Framework and implements the Queue interface while extending the AbstractCollection class.

public class PriorityQueue<E> extends AbstractQueue<E> implements java.io.Serializable                       

The AbstractQueue class supplies implementations for several methods of the Queue interface, giving you a skeletal implementation of that interface. Its purpose is to reduce the effort needed to implement the Queue interface.
The AbstractQueue class implements the Queue interface and extends the AbstractCollection class. It provides implementations for methods such as add, remove, element and offer.

Methods of the AbstractQueue class:

The add(E e) method

This method adds the given element to the queue and returns true when the insertion succeeds.
throws IllegalStateException, if the element cannot be added because of capacity restrictions.
throws ClassCastException, if the class of the given element prevents it from being added to the queue.
throws NullPointerException, if the queue does not allow null and the given element is null.
throws IllegalArgumentException, if some property of the given element prevents it from being added to the queue.

boolean add(E e);

Here, E is the type of elements held by the Queue.
e is the element you want to add to this Queue.
return type:  the method returns a boolean.

import java.util.LinkedList;
import java.util.Queue;
public class ExampleOfQueue 
{
	public static void main(String[] args) {
				Queue<String> listOfNames = new LinkedList<String>();
				listOfNames.add("JAVA");
				listOfNames.add("GOAL");
				listOfNames.add("RAVI");
				// Adding Same element again
				boolean isSuccessFullyAdded = listOfNames.add("RAVI");
				System.out.println("Element added again = "+ isSuccessFullyAdded);
				for(String name : listOfNames)
				{
					System.out.println("Names from list = "+name);
				}
	}
}

Output: Element added again = true
Names from list = JAVA
Names from list = GOAL
Names from list = RAVI
Names from list = RAVI

The addAll(Collection c) method

This method adds every element of the given collection to the queue and returns true when the insertion succeeds.
throws IllegalStateException, if the collection's elements cannot be added because of capacity restrictions.
throws ClassCastException, if the class of the given collection prevents it from being added to the queue.
throws NullPointerException, if the queue does not allow null and the given collection is null.
throws IllegalArgumentException, if some property of the given collection prevents it from being added to the queue.
return true, once the given collection has been added.

boolean addAll(Collection c);

Here, c is the collection whose elements are to be added to the Queue.
return type,  the method returns a boolean.

import java.util.LinkedList;
import java.util.Queue;
public class ExampleOfQueue 
{
	public static void main(String[] args) {
				Queue<String> listOfNames = new LinkedList<String>();
				listOfNames.add("JAVA");
				listOfNames.add("GOAL");
				listOfNames.add("RAVI");
				Queue<String> listOfNames2 = new LinkedList<String>();
				listOfNames2.add("NEW");
				listOfNames2.add("LIST");
				listOfNames.addAll(listOfNames2);
				for(String name : listOfNames)
				{
					System.out.println("Names from list = "+name);
				}
	}
}

Output: Names from list = JAVA
Names from list = GOAL
Names from list = RAVI
Names from list = NEW
Names from list = LIST

The remove() method

This method retrieves and removes the first element of the Queue. Its return type is E.

E remove();

Here, E is the element you want to remove from the Queue.
throws NoSuchElementException if this list is empty.

import java.util.LinkedList;
import java.util.Queue;
public class ExampleOfQueue 
{
	public static void main(String[] args) {
				Queue<String> listOfNames = new LinkedList<String>();
				listOfNames.add("JAVA");
				listOfNames.add("GOAL");
				listOfNames.add("RAVI");
				String removedName = listOfNames.remove();
				System.out.println("The element is removes successfully = "+removedName);
				for(String name : listOfNames)
				{
					System.out.println("Names from list = "+name);
				}
	}
}

Output: The element is removes successfully = JAVA
Names from list = GOAL
Names from list = RAVI

The element() method

This method returns the first element of the Queue without removing it from the list. It returns the object.

E element();

Here, E is the type of elements held by the Queue.
return, the first element of the Queue.
throws NoSuchElementException if this list is empty.

import java.util.LinkedList;
import java.util.Queue;
public class ExampleOfQueue 
{
	public static void main(String[] args) {
				Queue<String> listOfNames = new LinkedList<String>();
				listOfNames.add("JAVA");
				listOfNames.add("GOAL");
				listOfNames.add("RAVI");
				// It returns the element present at first position
				System.out.println("Element present at first position is = "+listOfNames.element());
	}
}

Output: Element present at first position is = JAVA

The clear() method

This method removes every element from the Queue. Its return type is void, which means it does not return anything.

void clear();
import java.util.LinkedList;
import java.util.Queue;
public class ExampleOfQueue 
{
	public static void main(String[] args) 
	{
		Queue<String> listOfNames = new LinkedList<String>();
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
		listOfNames.add("RAVI");
		listOfNames.add("SITE");
		// Before clear() method
		for(String name : listOfNames)
		{
			System.out.println("Name from list = "+name);
		}
		// Removing all the element by clear() method
		listOfNames.clear();
		System.out.println("After Clear() method list is empty : "+ listOfNames.isEmpty());
	}
}

Output: Name from list = JAVA
Name from list = GOAL
Name from list = RAVI
Name from list = SITE
After Clear() method list is empty : true

Leave a Comment

Because AbstractQueue already handles the shared Queue behaviour, you only need to supply the storage logic when you build your own queue class on top of it. The examples above use LinkedList, but the same methods are available on other Queue implementations such as PriorityQueue, so the patterns carry over directly.