AbstractQueue in Java and Queue Implementations

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 = RAVI

addAll(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 = LIST

remove()

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 = RAVI

element()

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 = JAVA

clear()

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 : true

Continue Learning

Read up on the related Queue interface topic to strengthen your grasp of Java collections.

Queue Interface Leave a Comment

Stay Connected

Chat Now Facebook Home

Browse Java Topics

Java Fundamentals

Control Structures

OOPs Concepts

Keywords 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.