AbstractQueue class

The AbstractQueue class is a part of the Java Collection Framework which implements the Queue interface and extends the AbstractCollection class.

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

The AbstractQueue class implements some methods of Queue interface and provides a skeletal implementation of the Queue interface. This class is used to minimize the effort required to implement the Queue interface.
The AbstractQueue class implements the Queue interface and AbstractQueue class extends the AbstractCollection class. This class provides implementation to some methods add, remove, element, offer etc.

Methods in AbstractQueue:

add(E e) method

This method inserts the specified element into the queue and returns true if the element adds successfully.
throws IllegalStateException, if the element not added successfully due to capacity restrictions.
throws ClassCastException, if the class of the specified element prevents adding in the queue.
throws NullPointerException, If Queue doesn’t allow Null and the specified element is null.
throws IllegalArgumentException, if some property of specified element prevents it from being added to the queue.

boolean add(E e);

Where, E represents the type of elements in Queue.
e is the element which you want to add in this Queue.
return type:  Its return type is 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

addAll(Collection c) method

This method inserts all elements of the specified collection into the queue and returns true if the element adds successfully.
throws IllegalStateException, if the elements of collection not added successfully due to capacity restrictions.
throws ClassCastException, if the class of the specified collection prevents adding in the queue.
throws NullPointerException, If Queue doesn’t allow Null and the specified collection is null.
throws IllegalArgumentException, if some property of specified collection prevents it from being added to the queue.
return true, after adding the specified collection.

boolean addAll(Collection c);

Where, c collection containing elements to be added to Queue.
return type,  its return type is 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

remove() method

This method is used retrieve and remove the first element from Queue. Its return type is E.

E remove();

Where, E is the element which 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

element() method

This method is used to get the first element from the Queue. But doesn’t remove the element from the list. It returns the object.

E element();

Where, E represents the type of elements in Queue.
return, First element 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");
				
				// 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

clear() method

This method is used to remove all the elements from the Queue. Its return type is void. It means this method doesn’t 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