A Queue is an in-built interface in the Collection framework. It can be used to store a group of objects. In this article, we will be learning more about Queues and understanding the various operations that can be performed on a Queue.

  1. What is a Queue
  2. Creating a Queue
  3. Adding Data to a Queue
  4. Removing Data from a Queue
  5. Retrieving Data from a Queue
  6. Determining Queue Size
  7. Queue Iteration

Java Queues

What is a Queue

As mentioned earlier, Queue is an interface in the Collection framework. It is a sub-interface of java.util.Collection just like Set and List. It can be used to store a group of objects. However, unlike a Set and List, it allows adding elements to the end of the queue (tail of the queue) and retrieving elements from the start of the queue (head of the Queue). It also has methods that can be used to inspect the head of the Queue.

Creating a Queue

In order to create a Queue, you need to create an object of any of the classes that implement the Queue interface like PriorityQueue, ArrayBlockingQueue, LinkedList, etc. You also need to specify the data type of the values being stored in the Queue via generics.

Code Sample

Queue<String> queue1 = new LinkedList<String>();
Queue<Double> queue2 = new PriorityQueue<Double>();
Queue<Integer> queue3 = new ArrayBlockingQueue<Integer>(5);
  • Line 1 creates a Queue called queue1. It stores data of type String. It uses a LinkedList implementation
  • Line 2 creates a Queue called queue2. It stores data of type Double. It uses an PriorityQueue implementation
  • Line 3 creates a Queue called queue3. It stores data of type Integer. It uses an ArrayBlockingQueue implementation

Adding Data to the Queue

There are two ways to add data to a Queue. There is a method called add on the Queue interface. It can be used to add data to the queue. It adds the specified element to the tail(end) of the Queue. If it is not possible to add data to the queue due to capacity restrictions or some other reason, it throws an Exception.

Code Sample

Queue<String> queue1 = new ArrayBlockingQueue<String>(3);
queue1.add("Rose");
queue1.add("Lily");
queue1.add("Tulip");
queue1.add("Sunflower");
System.out.println(queue1);
  • Line 1 creates a Queue called queue1 that stores data of type String. It uses an ArrayBlockingQueue implementation, specifying the size as 3. So, this means that this Queue cannot store more than 3 elements
  • Lines 2-5 invoke the add method to add some data to queue1
  • Lines 2-4 get executed successfully and these values get added to the Queue. At this point the Queue becomes full
  • When Line 5 is executed the Queue is already full, so the add method causes an exception.

Output

Exception in thread “main” java.lang.IllegalStateException: Queue full

There is another method on the queue interface called offer. Like add, it also adds the specified element to the tail of the queue. However, it differs from add in that, if it is unable to add data to the Queue due to capacity restrictions, it returns a false value.

Code Sample

Queue<String> queue1 = new ArrayBlockingQueue<String>(3);
queue1.offer("Rose");
queue1.offer("Lily");
queue1.offer("Tulip");
queue1.offer("Sunflower");
System.out.println(queue1);
  • In this case, Lines 2-5 invoke the offer method to add some data to queue1
  • Lines 2-4 get executed successfully and these values get added to the Queue. At this point the Queue becomes full
  • When Line 5 is executed the Queue is already full, so the offer method returns a false and the value “Sunflower” does not get added to the Queue

Output

[Rose, Lily, Tulip]

Removing Data from a Queue

There are two ways to remove an element from a Queue. There is a method called remove on the Queue interface. It can be used to remove data from the queue. It retrieves and removes the element at the head (beginning) of the Queue. If it is not possible to remove the element at the head of the queue due to the queue being empty, it throws a NoSuchElementException

Code Sample

Queue<String> queue1 = new LinkedList<String>();
queue1.add("Rose");
String flower = queue1.remove();
System.out.println("flower:"+flower);
flower = queue1.remove();
System.out.println("flower:"+flower);
  • Line 1 creates a String Queue called queue1 and Line 2 adds the value “Rose” to queue1
  • Line 3 invokes the remove method on queue1. This causes the value “Rose” to be removed and it is assigned to flower. At this point the Queue becomes empty.
  • Line 5 again invokes the remove method on queue1. Since the queue is empty, it causes a NoSuchElementException

Output

flower:Rose
Exception in thread “main” java.util.NoSuchElementException

There is another method on the queue interface called poll. Like remove, it also retrieves and removes the element at the head of the queue. However, it differs from remove in that, if it is unable to remove the element it returns a null.

Code Sample

Queue<String> queue1 = new LinkedList<String>();
queue1.add("Rose");
String flower = queue1.poll();
System.out.println("flower:"+flower);
flower = queue1.poll();
System.out.println("flower:"+flower);
  • In this case, Line 3 invokes the poll method on queue1. This causes the value “Rose” to be removed and it is assigned to flower. At this point the Queue becomes empty.
  • Line 5 again invokes the poll method on queue1. Since the queue is empty, it returns a null value.

Output

flower:Rose
flower:null

Retrieving Data from a Queue

As mentioned earlier, a queue allows retrieving the element at the head of the queue. Again, there are two ways to retrieve the head of the Queue. There is a method called element. This returns but does not remove the element at the head (beginning) of the Queue. If it is not possible to return the element at the head of the queue due to the queue being empty, it throws a NoSuchElementException

Code Sample

Queue<String> queue1 = new LinkedList<String>();
queue1.add("Rose");
String flower = queue1.element();
System.out.println("flower:"+flower);
queue1 = new LinkedList&amp;lt;String&amp;gt;();
flower = queue1.element();
System.out.println("flower:"+flower);
  • As before, Line 1 creates a String Queue called queue1 and Line 2 adds the value “Rose” to queue1
  • Line 3 invokes the element method on queue1. This returns the value “Rose” and assigns it to flower
  • Line 5 initializes queue1 again with another LinkedList.
  • Line 4 again invokes the element method on queue1. Since queue1 is now empty, it causes a NoSuchElementException

Output

flower:Rose
Exception in thread “main” java.util.NoSuchElementException

There is another method on the queue interface called peek. Like element, it also retrieves the element at the head of the queue. However, it differs from element in that, if it is unable to remove the element at the head, it returns a null.

Code Sample

Queue<String> queue1 = new LinkedList<String>();
queue1.add("Rose");
String flower = queue1.peek();
System.out.println("flower:"+flower);
queue1 = new LinkedList&lt;String&gt;();
flower = queue1.peek();
System.out.println("flower:"+flower);
  • In this case, Line 3 invokes the peek method. This returns the value “Rose” and assigns it to flower
  • Line 5 initializes queue1 again with another LinkedList and Line 6 invokes the peek method again. Since queue1 is now empty, peek returns a null.

Output

flower:Rose
flower:null

Determining Queue Size

The Queue interface has a method called size. This returns the size of the Queue

Code Sample

Queue<String> queue1 = new LinkedList<String>();
queue1.offer("Rose");
queue1.offer("Lily");
queue1.offer("Tulip");
int size = queue1.size();
System.out.println("size:"+size);
  • This code creates a queue1 and adds some values to it via the offer method.
  • Line 5 invokes the size method to obtain the Queue size

Output

size:3

Queue Iteration

The for-each loop can be used to iterate over a Queue.

Code Sample

Queue<String> queue1 = new LinkedList<String>();
queue1.offer("Rose");
queue1.offer("Lily");
queue1.offer("Tulip");
for(String flower:queue1) {
 System.out.println("flower is:"+flower);
}
  • This code creates a queue1 and adds some values to it via the offer method.
  • Line 5 uses a for-each loop that retrieves each element from the Queue and prints it

Output

flower is:Rose
flower is:Lily
flower is:Tulip

Conclusion

So, in this article, we learned about Java Queues and some of the operations that can be performed on a Queue. We saw how Queues only allow adding data to the tail of the queue and retrieving data from the head of the Queue. We also saw how data can be added/removed and inspected from the Queue. Finally, we saw how to determine the size of the Queue and how to iterate over the Queue.