TechiePassion is reader-supported. As an Amazon Associate I earn from qualifying purchases.

Java Queues: An In-Depth Article in 2024

java-queue

Developers can find Java Queue interface the java.util package. The interface inherits all the methods from the java.util.Collection interface. It means that the Queue interface can use all the methods of Collection interface. The Java Queue interface is used to insert and remove the elements but I use different methods for the same in comparison to other classes in the package.

Things to Remember about Java Queue

Here are some things about the Java Queue interface, which one should always keep in mind.

  • Java Queue takes the methods from the Collection interface as it extends the Collection interface.
  • The interface inserts the elements at the end and removes the elements from the first.
  • The interface is implemented by ArrayBlockingQueue, Priority Queue and LinkedList.
  • BlockingQueue throws NullPointerException as it does not accept any null value.
  • The Queues present in java.util package are unbounded queues while those present in java.util.concurrent package are bounded queues.
  • Deques is a queue that does not insert the elements in the last and removed from the first. It can be used to insert and remove elements from both ends.

Methods Included in Java Queue

Here is the list of the methods, which are included in the Java Queue interface.

int size()

This method is used to count how many elements are there in a set.

boolean isEmpty()

This method is used to check whether the set contains any element or is empty.

boolean contains(Object o)

If the element specified in the parameter is available, then the method returns true else it returns false.

Iterator iterator()

This method returns iterator and the elements can be retrieved in any order.

boolean removeAll(Collection c)

This method is used to remove all the elements in a specified set.

boolean retainAll(Collection c)

This method retains all the elements of a specified set.

void clear()

The method is used to clear a set by removing all its elements.

E remove()

This method gets the elements from the head of a queue and removes it.

E poll()

This method does the same thing as the E remove() method but it also returns null if no value is found.

E peek()

The developers can use this method to get  the elements present in a set. The value is null if there is no element present.

boolean offer(E e)

This method is used to insert an element in a queue.

E element()

It retrieves the elements from a queue.

boolean add(E e)

It adds an element in the queue but throws IllegalStateException if there is no space for the element.

Object[] toArray()

It returns an array of all elements. in a set in the same order in which they are inserted.

There are many operations that this interface can perform and we will see some of the examples here. To know more about arrays in Java read our latest article dedicated to it.

ADD Operation

The add() operation is used to add an element in a queue. The method returns true if the operation is performed successfully else IllegalStateException is thrown. Here is an example of adding elements in a queue.

import java,util.concurrent.*;
class AddExample

{
public static void main(String args[])
{
BlockingQueue<string> myqueue = new ArrayBlockingQueue();
System.out.println(queue.add(“first”));
System.out.println(queue.add(“second”));
System.out.println(queue.add(“third”));
System.out.println(queue);
}
}

The OUTPUT will be:

true
true
true
[first, second, third]

OFFER Operation

This operation is used to insert an element in a queue. In the example below, we will see how this operation works.

import java,util.concurrent.*;
class OfferExample
{
public static void main(String args[])
{
BlockingQueue<string> myqueue = new ArrayBlockingQueue();
System.out.println(queue.offer(“first”));
System.out.println(queue. offer (“second”));
System.out.println(queue. offer (“third”));
System.out.println(queue);
}
}

The OUTPUT will be:

true
true
true
[first, second, third]

DELETE Operation

The delete operation removes the elements from the head of a queue and returns true if the operation is conducted successfully. Here are some examples, which will let the users know how it works.

There are two methods used for the operation which are remove() and poll(). We will see the example of each.

Here is an Example of Using remove() Method.

import java,util.concurrent.*;
class RemoveExample
{
public static void main(String args[])
{
Queue<string> myqueue = new ArrayBlockingQueue();
queue.offer(“first”);
offer (“second”);
System.out.println(queue);
System.out.println(queue. remove ());
System.out.println(queue. remove ());
}
}

The OUTPUT will be:

[first, second]
first
second

All the elements will be removed from the queue after the second remove operation.

Here is the Example of poll() Operation.

import java,util.concurrent.*;
class PollExample
{
public static void main(String args[])
{
Queue<string> myqueue = new ArrayBlockingQueue();
queue.offer(“first”);
offer (“second”);
System.out.println(queue);
System.out.println(queue. poll ());
System.out.println(queue. poll ());
}
}

The OUTPUT will be:

[first, second]
first
second

All the elements will be removed from the queue after the second poll operation.

Retrieve Operation

Developers can retrieve the elements from a queue through element90 and peek() methods. Let us see the usage of both these methods.

Example of element() Method

import java,util.concurrent.*;
class ElementExample
{
public static void main(String args[])
{
Queue<string> myqueue = new ArrayBlockingQueue();
queue.add(“first”);
System.out.println(queue.element());
System.out.println(queue);
queue.clear()
System.out.println(queue.element());
}
}

The OUTPUT will be:

[first]
Exception in thread "main" java.util.NoSuchElementException

Example of peek() OPERATION

import java,util.concurrent.*;
class PeekExample
{
public static void main(String args[])
{
Queue<string> myqueue = new ArrayBlockingQueue();
queue.add(“first”);
System.out.println(queue.peek());
System.out.println(queue);
queue.clear()
System.out.println(queue.element());
}
}

The OUTPUT will be:

[first]
Null

Java Queue Categories

There are two types of queues, which developers can implement and these include bounded queues and unbounded queues. Developers have to define a size for the bounded queues while this is not needed for unbounded queues. Queues available in java.util package are unbounded queues while those of java.util.concurrent package are bounded.

The queues can also be divided into blocking queues and non-blocking queues. The classes that implement BlockingQueue interface are blocking queues and rest are non-blocking ones.

Wrapping Up

Users can use the Queue interface for adding, inserting, retrieving, and deleting elements from a queue. So they can be linked with any collection class of java.util package and then all these operations can be performed.

The methods available in the Queue are inherited from Collection interface so users can use all the methods to carry out various types of operations and get the required output. The interface usage is very easy and can be used with any class. To know more about Java Language you can check this out.