0% found this document useful (0 votes)
3 views5 pages

Java Queue Interface Overview and Usage

The Java Queue interface is part of the collections framework and provides queue data structure functionalities, extending the Collection interface. Implementations of the Queue interface include ArrayDeque, LinkedList, and PriorityQueue, while it is extended by subinterfaces such as Deque and BlockingQueue. Common methods of the Queue interface include add(), offer(), element(), peek(), remove(), and poll(), which facilitate element management in a First In, First Out manner.

Uploaded by

cecilia priscila
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Java Queue Interface Overview and Usage

The Java Queue interface is part of the collections framework and provides queue data structure functionalities, extending the Collection interface. Implementations of the Queue interface include ArrayDeque, LinkedList, and PriorityQueue, while it is extended by subinterfaces such as Deque and BlockingQueue. Common methods of the Queue interface include add(), offer(), element(), peek(), remove(), and poll(), which facilitate element management in a First In, First Out manner.

Uploaded by

cecilia priscila
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Queue Interface

The Queue interface of the Java collections framework provides the


functionality of the queue data structure. It extends
the Collection interface.

Classes that Implement Queue


Since the Queue is an interface, we cannot provide the direct
implementation of it.
In order to use the functionalities of Queue , we need to use classes that
implement it:
 ArrayDeque
 LinkedList
 PriorityQueue

Interfaces that extend Queue


The Queue interface is also extended by various subinterfaces:
 Deque

 BlockingQueue

 BlockingDeque

Working of Queue Data Structure


In queues, elements are stored and accessed in First In, First
Out manner. That is, elements are added from the behind and removed
from the front.

How to use Queue?


In Java, we must import [Link] package in order to use Queue .
// LinkedList implementation of Queue
Queue<String> animal1 = new LinkedList<>();

// Array implementation of Queue


Queue<String> animal2 = new ArrayDeque<>();

// Priority Queue implementation of Queue


Queue<String> animal3 = new PriorityQueue<>();

Here, we have created objects animal1 , animal2 and animal3 of


classes LinkedList , ArrayDeque and PriorityQueue respectively. These
objects can use the functionalities of the Queue interface.

Methods of Queue
The Queue interface includes all the methods of the Collection interface. It
is because Collection is the super interface of Queue .

Some of the commonly used methods of the Queue interface are:


 add() - Inserts the specified element into the queue. If the task is
successful, add() returns true , if not it throws an exception.
 offer() - Inserts the specified element into the queue. If the task is
successful, offer() returns true , if not it returns false .

 element() - Returns the head of the queue. Throws an exception if the


queue is empty.
 peek() - Returns the head of the queue. Returns null if the queue is
empty.
 remove() - Returns and removes the head of the queue. Throws an
exception if the queue is empty.
 poll() - Returns and removes the head of the queue. Returns null if the
queue is empty.
Implementation of the Queue Interface
1. Implementing the LinkedList Class
import [Link];
import [Link];

class Main {

public static void main(String[] args) {


// Creating Queue using the LinkedList class
Queue<Integer> numbers = new LinkedList<>();

// offer elements to the Queue


[Link](1);
[Link](2);
[Link](3);
[Link]("Queue: " + numbers);

// Access elements of the Queue


int accessedNumber = [Link]();
[Link]("Accessed Element: " + accessedNumber);

// Remove elements from the Queue


int removedNumber = [Link]();
[Link]("Removed Element: " + removedNumber);

[Link]("Updated Queue: " + numbers);


}

Output

Queue: [1, 2, 3]
Accessed Element: 1
Removed Element: 1
Updated Queue: [2, 3]

To learn more, visit Java LinkedList.

2. Implementing the PriorityQueue Class


import [Link];
import [Link];

class Main {

public static void main(String[] args) {


// Creating Queue using the PriorityQueue class
Queue<Integer> numbers = new PriorityQueue<>();

// offer elements to the Queue


[Link](5);
[Link](1);
[Link](2);
[Link]("Queue: " + numbers);

// Access elements of the Queue


int accessedNumber = [Link]();
[Link]("Accessed Element: " + accessedNumber);

// Remove elements from the Queue


int removedNumber = [Link]();
[Link]("Removed Element: " + removedNumber);

[Link]("Updated Queue: " + numbers);


}
}

Output

Queue: [1, 5, 2]
Accessed Element: 1
Removed Element: 1
Updated Queue: [2, 5]

You might also like