AbstractQueue in Java: A Skeletal Queue Implementation
AbstractQueue is part of the Java Collection Framework. It implements the Queue interface and extends AbstractCollection, so it hands developers a ready-made base for creating their own queue types. Since it offers a skeletal implementation of the Queue interface, you write far less code when defining custom queue classes.
Put simply, AbstractQueue implements Queue and extends AbstractCollection, and it already supplies implementations for several core operations, including add, remove, element, and offer.
Signature and Inheritance
The class declaration follows the pattern shown below, where E is the type of elements the queue holds:
public class PriorityQueue<E> extends AbstractQueue<E> implements java.io.Serializable
This structure illustrates how a concrete queue such as PriorityQueue can build on the skeletal behavior that AbstractQueue provides.
Methods That AbstractQueue Provides
add(E e)
This method adds the given element to the queue and returns true when the insertion succeeds. It can throw IllegalStateException if capacity limits stop the element from being added, ClassCastException if the element's class is not allowed, NullPointerException if the queue disallows null and the element is null, or IllegalArgumentException if some property of the element prevents insertion.
boolean add(E e);
Here E is the element type and e is the element being inserted. The method returns a boolean.
Example:
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");
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)
This method adds every element of the supplied collection to the queue and returns true when the operation succeeds. It can throw IllegalStateException if capacity limits block the collection's elements, ClassCastException if the collection's class is not allowed, NullPointerException if the queue disallows null and the collection is null, or IllegalArgumentException if some property of the collection prevents insertion.
boolean addAll(Collection c);
Here c is the collection whose elements get added. The method returns a boolean.
Example:
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()
This method fetches and removes the first element of the queue. It returns type E and throws NoSuchElementException when the queue is empty.
E remove();
Here E is the type of the element being removed.
Example:
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()
This method returns the first element of the queue without taking it out. It returns an object of type E and throws NoSuchElementException when the queue is empty.
E element();
Here E is the element type, and the method returns the queue's first element.
Example:
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");
System.out.println("Element present at first position is = " + listOfNames.element());
}
}
Output:
Element present at first position is = JAVA
clear()
This method removes all elements from the queue. It returns void, so it gives back nothing.
void clear();
Example:
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");
for (String name : listOfNames) {
System.out.println("Name from list = " + name);
}
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
Keep Exploring
Keep browsing the site or switch to the mobile-friendly version with the options below.
JavaGoal Exit mobile version Leave a CommentAbstractQueue sits between the Queue interface and concrete classes, so it is the natural starting point when you need a custom queue. Review the method examples above, then try them with a LinkedList to see how add, remove, element, and clear behave in practice.
