Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
AbstractQueue in Java with Examples
3.
Class Hierarchy
3.1.
Syntax
4.
Constructor in Java AbstractQueue
4.1.
Example
5.
Methods in Java AbstractQueue
5.1.
Example
6.
Basic Operations
6.1.
Adding Elements
6.1.1.
Example
6.2.
Remove the Elements  
6.2.1.
Example
6.3.
Accessing the Elements
6.3.1.
Example
7.
FAQs
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Abstract Queue

Introduction

The AbstractQueue class in Java implements the Collection interface and the AbstractCollection class as part of the Java Collection Framework. It has only rudimentary implementations of various Queue operations. When the underlying implementation does not allow null components, this class' implementations are suitable. The methods add, remove, and element are based on offer, poll, and peek, respectively, but instead of returning false or null, they throw exceptions.

Also read, Duck Number in Java and Hashcode Method in Java

AbstractQueue in Java with Examples

The AbstractQueue class implements the Collection interface and the AbstractCollection class in Java, which is part of the Java Collection Framework. It includes rudimentary implementations of certain Queue functions. When the base implementation does not accept null items, this class is appropriate.

Also see,  Swap Function in Java

Class Hierarchy

java.lang.Object
 ↳ java.util.AbstractCollection<E>
    â†³ Class AbstractQueue<E>

Syntax

public abstract class AbstractQueue<E>
  extends AbstractCollection<E>
     implements Queue<E>

E: Here is the type of element maintained by this collection class.

Constructor in Java AbstractQueue

Protected AbstractQueue(): The default constructor, but being protected, it doesn't allow the creation of an AbstractQueue object.

Example

import java.util.*; 
import java.util.concurrent.LinkedBlockingQueue; 
public class Constructor1 
{ 
    public static void main(String[] argv) 
        throws Exception 
    { 
        // Creating object of AbstractQueue<Integer> 
        AbstractQueue<Integer> 
            AQ = new LinkedBlockingQueue<Integer>(); 
  
        // Populating AQ 
        AQ.add(10); 
        AQ.add(20); 
        AQ.add(30); 
        AQ.add(40); 
        AQ.add(50); 
  
        // print AQ 
        System.out.println("AbstractQueue contains: "
                           + AQ); 
    } 
} 
You can also try this code with Online C Compiler
Run Code

 

Output

AbstractQueue contains: [10, 20, 30, 40, 50]
Above is a sample program to illustrate AbstractQueue in Java.
You can also try this code with Online C Compiler
Run Code


You can try it on java online compiler.

Methods in Java AbstractQueue

  1. Add (E e): If it is possible to insert the supplied element into this queue immediately without breaching capacity limitations, true is returned on success, and an IllegalStateException is thrown if no space is now available.
     
  2. addAll(Collection c): Adds all elements in the specified collection to this Queue.
     
  3. Clear (): This Queue's elements are all removed.
     
  4. Element (): The head of this Queue is retrieved but not removed.
     
  5. Remove (): This Queue's head is retrieved and removed.

Example

// Java code to illustrate
import java.util.*; 
import java.util.concurrent.LinkedBlockingQueue;  
public class Methods1 
{ 
    public static void main(String[] argv) 
        throws Exception 
    { 
        // Creating object of AbstractQueue<Integer> 
        AbstractQueue<Integer> 
            AQ = new LinkedBlockingQueue<Integer>(); 
  
        // Populating AQ using add() method 
        AQ.add(10); 
        AQ.add(20); 
        AQ.add(30); 
        AQ.add(40); 
        AQ.add(50); 
  
        // print AQ 
        System.out.println("AbstractQueue contains: "
                           + AQ); 
  
        // Get the head of Queue using element() method 
        System.out.println("Head: " + AQ.element()); 
  
        // Clear the Queue using clear() method 
        AQ.clear(); 
        System.out.println("AbstractQueue: " + AQ); 
    } 
} 
You can also try this code with Online C Compiler
Run Code

Output

AbstractQueue contains: [10, 20, 30, 40, 50]
Head: 10
AbstractQueue: []
You can also try this code with Online C Compiler
Run Code

Above is a sample program to methods of AbstractQueue in Java.

Basic Operations

Adding Elements

There are two techniques for adding components to the AbstractQueue. If it's possible to do it right away without going over capacity, the add(E e) method inserts the provided element into this queue. If there is currently no space available, it returns true and throws an IllegalStateException. The addAll(E e) method populates this Queue with all of the elements in the provided collection.

Example

// Java program to illustrate the
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue; 
public class AddingElementsExample {
 
    public static void main(String[] args) throws Exception
    {
        AbstractQueue<Integer> AQ1 = new LinkedBlockingQueue<Integer>();
 
        // Populating AQ
        AQ1.add(5);
        AQ1.add(10);
        AQ1.add(15);
        AQ1.add(20);
        AQ1.add(25);
        System.out.println("AbstractQueue contains : "
                           + AQ1);
        AbstractQueue<Integer> AQ2 = new LinkedBlockingQueue<Integer>();
        System.out.println("AbstractQueue2 initially contains : " + AQ2);
        AQ2.addAll(AQ1);
 
        System.out.println( "AbstractQueue1 after addition contains : " + AQ2);
    }
}
You can also try this code with Online C Compiler
Run Code

Output

AbstractQueue contains : [5,10,15,20,25]
AbstractQueue2 initially contains : []
AbstractQueue1 after addition contains : [5,10,15,20,25]
Above is a sample program for adding elements to the AbstractQueue in Java.
You can also try this code with Online C Compiler
Run Code

Remove the Elements  

It has remove() and clear() methods for removing elements from AbstractQueue.

The remove() method returns and deletes the Queue's head.

This Queue's elements are removed using the clear() method. When this call returns, the Queue will be empty.

Example

// Java program to illustrate the 
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
 
public class RemovingElementsExample {
    public static void main(String[] argv) throws Exception
    {
        AbstractQueue<Integer> AQ1 = new LinkedBlockingQueue<Integer>();
        AQ1.add(10);
        AQ1.add(20);
        AQ1.add(30);
        AQ1.add(40);
        AQ1.add(50);
        System.out.println("AbstractQueue1 contains : " + AQ1);
 
        int head = AQ1.remove();
 
        System.out.println("head : " + head);
 
        System.out.println("AbstractQueue1 after removal of head : " + AQ1);
 
        AQ1.clear();
        System.out.println("AbstractQueue1 : " + AQ1);
    }
}
You can also try this code with Online C Compiler
Run Code

Output

AbstractQueue1 contains : [10, 20, 30, 40, 50]
head : 10
AbstractQueue1 after removal of head : [20, 30, 40, 50]
AbstractQueue1 : []
You can also try this code with Online C Compiler
Run Code

Above is a sample program for removing elements from AbstractQueue in Java.

Accessing the Elements

The element() method of AbstractQueue retrieves the head of this Queue but does not remove it. 

Example

// Java program to illustrate the 
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
public class AccessingElementExample {
 
    public static void main(String[] args) throws Exception
    {
        // Since AbstractQueue is an abstract class
        // create object using LinkedBlockingQueue
        AbstractQueue<Integer> AQ1 = new LinkedBlockingQueue<Integer>();
 
        // Populating AQ1 using add method
        AQ1.add(10);
        AQ1.add(20);
        AQ1.add(30);
        AQ1.add(40);
        AQ1.add(50);
 
        // print AQ to the console
        System.out.println("AbstractQueue1 contains : " + AQ1);
 
        // access the head element
        System.out.println("head : " + AQ1.element());
    }
}
You can also try this code with Online C Compiler
Run Code

Output

AbstractQueue1 contains : [10, 20, 30, 40, 50]
head : 10
You can also try this code with Online C Compiler
Run Code

Above is a sample program for accessing an element from AbstractQueue in Java.

Check out this problem - Queue Implementation

FAQs

  1. Why is the Queue used?
    Ans: For synchronization, the queue data structure is utilized. The Queue is also used to schedule disc and CPU access.
     
  2. What is a Queue in Java?
    Ans: In Java, a queue is a linearly ordered data structure with entries ordered FIFO (First In, First Out). This indicates that the element in the Queue that was added initially will be the first to be withdrawn. In Java, a Queue is an interface that inherits from the Collection interface.
     
  3. What is the Unbounded Queue?
    Ans: In the unbounded Queue, we don't set the capacity of the Queue explicitly, and it can grow in size. The capacity is set to Integer.MAX_VALUE.

Conclusion

In this article, we have extensively discussed Abstract Queue and also learned about the method in Java AbstractQueue and basic operations.

"We hope that our blog enhances your knowledge regarding Abstract Queue, and if you would like to learn extra, check out our articles on Priority Queue in JavaApplications of Priority Queue. Do upvote this blog to help other ninjas grow. Happy Coding!"

Live masterclass