AbstractQueue in Java: A Skeletal Implementation of the Queue Interface
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 can reduce the effort required to build a custom queue.
Put simply, AbstractQueue implements Queue and inherits from AbstractCollection, and it already supplies implementations for methods such as add, remove, element, and offer.
Declaration and Class Signature
The class is declared as a generic type, as the signature below shows. Keep in mind that the sample signature references PriorityQueue, one of the concrete classes built on this framework.
public class PriorityQueue<E> extends AbstractQueue<E> implements java.io.Serializable
Methods Provided by AbstractQueue
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 blocks insertion, NullPointerException if the queue disallows null and the element is null, and IllegalArgumentException if some property of the element prevents it from being added.
boolean add(E e);
- E is the type of elements held by the queue.
- e is the element you want to add.
- The return type is 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");
// Adding the 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 = RAVIaddAll(Collection c)
This method adds every element of the supplied collection to the queue and returns true when the insertion succeeds. It can throw IllegalStateException if capacity limits block the collection's elements, ClassCastException if the collection's class prevents insertion, NullPointerException if the queue disallows null and the collection is null, and IllegalArgumentException if some property of the collection prevents insertion. It returns true once the collection has been added.
boolean addAll(Collection c);
- c is the collection holding the elements to add to the queue.
- The return type is 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 = LISTremove()
This method fetches and removes the first element of the queue. Its return type is E. It throws NoSuchElementException when the queue is empty.
E remove();
- E is the element type you want to remove from the queue.
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 removed successfully = " + removedName);
for (String name : listOfNames) {
System.out.println("Names from list = " + name);
}
}
}Output:
The element is removed successfully = JAVA
Names from list = GOAL
Names from list = RAVIelement()
This method returns the first element of the queue without removing it, giving back the object itself. It throws NoSuchElementException when the queue is empty.
E element();
- E is the type of elements held by the queue.
- It returns the first element of the queue.
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");
// Returns the element at the first position
System.out.println("Element present at first position is = " + listOfNames.element());
}
}Output:
Element present at first position is = JAVAclear()
This method deletes every element from the queue. Its return type is void, so it returns 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");
// Before clear()
for (String name : listOfNames) {
System.out.println("Name from list = " + name);
}
// Removing all elements with clear()
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 : trueContinue Learning
Read up on the related Queue interface topic to strengthen your grasp of Java collections.
Queue Interface Leave a CommentStay Connected
Chat Now Facebook HomeBrowse Java Topics
Java Fundamentals
Control Structures
- Control Structures in Java
- Java Control Statements
- if Statement in Java
- Switch Statement
- Java Loops
- break Statement in Java
- continue Statement in Java
- Array in Java
OOPs Concepts
- OOPs Concepts in Java
- Class and Object in Java
- Constructor in Java
- Constructor Overloading in Java
- Constructor Chaining in Java
- Inheritance in Java (Is-A Relationship)
- Multiple Inheritance in Java
- Polymorphism in Java
- Method Overloading in Java
- Method Overriding in Java
- Abstraction in Java
- Abstract Class in Java
- Interface in Java
- Multiple Inheritance Using Interface in Java
- Encapsulation in Java
- Association in Java
- Access Modifiers in Java
- Package in Java
- Java import Package
- enum in Java
- Wrapper Class in Java
- Immutable Class in Java
Keywords in Java
- Final Keyword in Java
- final Variable in Java
- final Method in Java
- final Class in Java
- final, finally and finalize
- static Keyword in Java
- Static Variable in Java
- Static Method in Java
- Static Block in Java
- Static Class in Java
- this Keyword in Java
- Super Keyword in Java
- import in Java
- Nested Classes in Java
Multithreading
AbstractQueue is a convenient starting point whenever you need a custom queue: it handles the core Queue contract while you focus on the storage details. Pair it with the concrete implementations covered here, such as PriorityQueue and LinkedList, to see how the same methods behave in practice.
