Core Java

Java Queue Interface

Introduction:

A Queue is a FIFO (First In First Out) abstract data type (ADT). In other words, the elements are removed in the order in which they were inserted.

The java.util.Queue is an interface in Java and extends from java.util.Collection. Some of the commonly used Queue implementation classes include a LinkedList, an ArrayDeque and a PriorityQueue.

Types of Queues:

There are two major categories of the queue:

1. Blocking queues: These are the bounded queues having a fixed capacity. That means we must provide the capacity of the queue at the time of its creation.

The implementation classes for bounded queues are present in java.util.concurrent package. For example, an ArrayBlockingQueue.

2. Non-Blocking Queues: The queue implementations under java.util package comes under this category of queues. For example, a LinkedList or a PriorityQueue.

As the name suggests, these are unbounded and we can insert as many elements as we want.

Key Methods:

Let’s look at the most common methods exposed by the Queue interface:

Throws An Exception:

Below methods will attempt to operate on the queue. If they fail for some reason, say due to capacity issues, they’ll throw a runtime exception:

1. add():

With add(), we can add an element to the queue:

Queue<Integer> queue = new LinkedList<>();
 
queue.add(1);
queue.add(2);
queue.add(3);

If the element insertion was successful, it returns true. Or else, it throws an IllegalStateException.

2. remove():

We can use remove() method to retrieve and remove an element from the head of the queue:

int removedItem = queue.remove();
 
System.out.println(removedItem); //prints 1
 
System.out.println(queue); // [2, 3]

Note that we removed the first element from the queue. So, now our queue contains [2, 3] only.

This method will throw a NoSuchElementException when invoked on an empty queue.

3. element():

This method will only return the head element of the queue, without removing it:

int topItem = queue.element(); // 2
 
System.out.println(queue); //[2, 3]

Note that our queue still contains two elements.

Just like remove(), it’ll throw a NoSuchElementException exception for an empty queue.

Returns Special Values:

We’ll now cover the slight variations of the above methods. These group of methods won’t throw an exception. They’ll rather return some value when they fail to perform an operation:

1. offer():

Just like add(), we have an offer() method which we can use to insert an element:

Queue<Integer> queue = new LinkedList<>();
queue.offer(1);
queue.offer(2);
queue.offer(3);

When failing to insert, it’ll just return false.

2. poll():

We can use poll() to retrieve and remove an element:

int removedItem = queue.poll(); // returns 1
 
System.out.println(queue); //[2, 3]

For an empty queue, it’ll simply return a null value.

3. peek():

Similar to element(), it just retrieves the front element of the queue:

int topItem = queue.peek(); // 2
 
System.out.println(queue); //still [2, 3]

Since we have already removed 1, the top element here is 2.

We’ll get a null value if we use this method on an empty queue.

Other Useful Methods:

Since Queue extends from the Collection interface, it also inherits methods from that interface. Some of the useful ones include:

1. size(): Returns the size of the queue

2. contains(): Returns true, if the element exists in the queue

3. isEmpty(): For an empty queue, it will return true. Or else, false.

Conclusion:

In this article, we talked about the Queue interface in Java. We covered all the primary methods exposed by this interface.

Be the First to comment.

Published on Java Code Geeks with permission by Shubhra Srivastava, partner at our JCG program. See the original article here: Java Queue Interface

Opinions expressed by Java Code Geeks contributors are their own.

Shubhra Srivastava

Shubhra is a software professional and founder of ProgrammerGirl. She has a great experience with Java/J2EE technologies and frameworks. She loves the amalgam of programming and coffee :)
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button