Java Queue Example (with video) (original) (raw)

In this article, we feature a comprehensive Java Queue Example and we explain what a Priority Queue in Java is. With the Java Queue, we can keep and handle elements before processing. Except for the methods that Collection provides, it also supports some basic operations in order to simulate the classic queue structure. Each of these operations exists in two forms:

You can also check this tutorial in the following video:

Java Queue Example – Video

It is worth to mention that FIFO (first-in-first-out) is the most common way to order the elements in the Java Queue. In order to visualize the structure of the Queue please consider the snapshot below,

Java Queue - Data Structure

Queue Data Structure

The above snapshot shows that the queue is merely acting as a buffer which can be filled from one end, with operation enqueue, and emptied from the other end, with operation dequeue.

Queue data structure implementation comes baked in Java Collections Framework, the following diagram shows the place of the Queue implementation class within the hierarchy of the java collections framework.

Java Queue - UML Diagram

UML Diagram for Queue Data Structure

In the following section, we are going to show the operations to manage a queue.

1. Explanation of Queue operations

First of all, let’s see analytically the basic operations that exist in two different forms.

1.1. Throws exception

1.2. Returns special value

2. Queue’s Interfaces

The Queue interface does not define the blocking queue methods, which are common in concurrent programming. These methods, which wait for elements to appear or for space to become available, are defined in the BlockingQueue interface, which extends this interface

3. Queue’s Implementations

Queue implementations generally do not allow insertion of null elements, although some implementations, such as LinkedList, do not prohibit insertion of null. Even in the implementations that permit it, null should not be inserted into a Queue, as null is also used as a special return value by the poll method to indicate that the queue contains no elements.

Queue implementations generally do not define element-based versions of methods equals and hashCode but instead inherit the identity-based versions from class Object, because element-based equality is not always well-defined for queues with the same elements but different ordering properties.

3.1. General-purpose Queue implementations:

3.2. Concurrent Queue implementations:

4. Deque’s Implementations

The Deque interface, pronounced as “deck”, represents a double-ended queue. The Deque interface can be implemented as various types of Collections. The Deque interface implementations are grouped into general-purpose and concurrent implementations.

4.1 General-purpose Queue implementations:

4.2 Concurrent Queue implementations:

While Deque implementations are not strictly required to prohibit the insertion of null elements, they are strongly encouraged to do so. Users of any Deque implementations that do allow null elements are strongly encouraged not to take advantage of the ability to insert nulls. This is so because null is used as a special return value by various methods to indicate that the deque is empty.

Deque implementations generally do not define element-based versions of the equals and hashCode methods, but instead inherit the identity-based versions from class Object.

Now, we are going to show in the code how to use the operations we explained above. So, create a new java file with the name QueueClass and paste the following code.

QueueClass.java

010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051 package com.javacodegeeks.core.queue;import java.util.LinkedList;import java.util.NoSuchElementException;import java.util.Queue;public class QueueClass { public static void main(String[] args) { Queue myQueue = new LinkedList(); myQueue.offer("Monday"); myQueue.offer("Thursday"); boolean flag = myQueue.offer("Wednesday"); System.out.println("Wednesday inserted successfully? "+flag); try { myQueue.add("Thursday"); myQueue.add("Friday"); myQueue.add("Weekend"); } catch (IllegalStateException e) { e.printStackTrace(); } System.out.println("Pick the head of the queue: " + myQueue.peek()); String head = null; try { head = myQueue.remove(); System.out.print("1) Push out " + head + " from the queue "); System.out.println("and the new head is now: "+myQueue.element()); } catch (NoSuchElementException e) { e.printStackTrace(); } head = myQueue.poll(); System.out.print("2) Push out " + head + " from the queue"); System.out.println("and the new head is now: "+myQueue.peek()); System.out.println("Does the queue contain 'Weekend'? " + myQueue.contains("Weekend")); System.out.println("Does the queue contain 'Monday'? " + myQueue.contains("Monday")); }}

As you can see in the code above, in order to create a queue we should assign LinkedList instance to the Queue object. In addition, you can notice how we call and use the functions we explained before. Also, it is worth to mention that you can use more methods that Queue inherits from Collection, such as contains() method.

You can see the output of the execution of the above code.

Output

Wednesday inserted successfully? true Pick the head of the queue: Monday

  1. Push out Monday from the queue and the new head is now: Thursday
  2. Push out Thursday from the queue and the new head is now: Wednesday Does the queue contain 'Weekend'? true Does the queue contain 'Monday'? false

In this next example of java queue, we will discuss the Java BlockingQueue. Java BlockingQueue implementations are thread-safe. All queuing methods are atomic in nature and use internal locks or other forms of concurrency control. Java BlockingQueue interface is part of java collections framework and it’s primarily used for implementing the producer-consumer problem. In the code example shown below, we will be using the ArrayBlockingQueue concrete class, which is one of the implementations of BlockingQueue Interface.

First we will define the structure of the object which will be passed between the Producer and Consumer.

MessageClass.java

public class MessageClass { private String messageString;

public MessageClass(String passedString) {
    this.messageString = passedString;
}

public String getMessageString() {
    return messageString;
}

}

Next, we will define the Producer class and Consumer class.

ProducerClass.java

import java.util.concurrent.BlockingQueue;

public class ProducerClass implements Runnable {

private BlockingQueue<MessageClass> queue;

public ProducerClass(BlockingQueue<MessageClass> q) {
    this.queue = q;
}

@Override
public void run() {
    for (int i = 0; i < 5; i++) {
        MessageClass msg = new MessageClass("" + i);
        try {
            Thread.sleep(i);
            queue.put(msg);
            System.out.println("Produced " + msg.getMessageString());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    MessageClass msg = new MessageClass("exit");
    try {
        queue.put(msg);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

}

ConsumerClass.java

import java.util.concurrent.BlockingQueue;

public class ConsumerClass implements Runnable {

private BlockingQueue<MessageClass> queue;

public ConsumerClass(BlockingQueue<MessageClass> q) {
    this.queue = q;
}

@Override
public void run() {
    try {
        MessageClass msg;
        while ((msg = queue.take()).getMessageString() != "exit") {
            Thread.sleep(10);
            System.out.println("Consumed " + msg.getMessageString());
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

}

Finally, we will define the driver class for the application.

BlockingQueueExample.java

import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue;

public class BlockingQueueExample { public static void main(String[] args) { BlockingQueue queue = new ArrayBlockingQueue<>(10); ProducerClass producer = new ProducerClass(queue); ConsumerClass consumer = new ConsumerClass(queue); new Thread(producer).start(); new Thread(consumer).start(); System.out.println("BlockingQueue Example"); System.out.println("Producer and Consumer has been started"); } }

This driver class initiates both the Producer and Consumer class objects. Producer will start producing in a thread safe way and Consumer consumes in similar manner as producer. Output of the BlockingQueueExample.java is shown in the snapshot below.

Java Queue - BlockingQueueExample.java

Output of BlockingQueueExample.java

This next example is about the BlockingDeque in Java. The BlockingDeque interface in the java.util.concurrent class represents a deque which is thread safe to put into, and take instances from. The BlockingDeque class is a Deque which blocks threads tring to insert or remove elements from the deque, in case it is either not possible to insert or remove elements from the deque. The LinkedBlockingDeque class implements the BlockingDeque interface. The LinkedBlockingDeque is a Deque which will block if a thread attempts to take elements out of it while it is empty, regardless of what end the thread is attempting to take elements from.

The code snippet showing the BlockingDeque in action is shown below.

BlockingDequeExample.java

import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque;

public class BlockingDequeExample { public static void main(String[] args) { System.out.println("Blocking DeQueue Example"); BlockingDeque LBD = new LinkedBlockingDeque<>();

    LBD.add(7855642);
    LBD.add(35658786);
    LBD.add(5278367);
    LBD.add(74381793);

    System.out.println("Blocking Deque1: "
            + LBD);
    System.out.println("Size of Blocking Deque1: "
            + LBD.size());

    BlockingDeque<Integer> LBD1
            = new LinkedBlockingDeque<>(3);

    LBD1.add(7855642);
    LBD1.add(35658786);
    LBD1.add(5278367);

    try {
        LBD1.add(74381793);
    }
    catch (Exception e) {
        System.out.println("Exception: " + e);
    }

    System.out.println("Blocking Deque2: "
            + LBD1);
    System.out.println("Size of Blocking Deque2: "
            + LBD1.size());

    BlockingDeque<Integer> LBD2
            = new LinkedBlockingDeque<>(LBD1);

    System.out.println("Blocking Deque3: "
            + LBD2);
}

}

In this code snippet, we are trying to add the elements to the blocking double ended queue and handle the exception when the count of elements exceeds the capacity of the deque. Output of BlockingDequeExample.java is shown below.

Java Queue - Output

Output of BlockingDequeExample.java

In this last example of java queue we will discuss the TransferQueue.

TransferQueue allows us to create programs according to the producer-consumer pattern, and coordinate messages passing from producers to consumers.

The implementation is actually similar to the BlockingQueue – but gives us the new ability to implement a form of back-pressure. This means that, when the producer sends a message to the consumer using the transfer() method, the producer will stay blocked until the message is consumed.

The LinkedTransferQueue class in Java is a part of the Java Collection Framework and implements the TransferQueue Interface.

The code snippet showing the TransferQueue in action is shown below.

TransferQueueExample.java

import java.util.concurrent.LinkedTransferQueue; import java.util.concurrent.TransferQueue;

public class TransferQueueExample { public static void main(String[] args) { System.out.println("Transfer Queue Example"); TransferQueue LTQ = new LinkedTransferQueue<>();

    LTQ.add(7855642);
    LTQ.add(35658786);
    LTQ.add(5278367);
    LTQ.add(74381793);

    System.out.println("Transfer Queue1: "
            + LTQ);

    TransferQueue<Integer> LTQ2
            = new LinkedTransferQueue<>(LTQ);

    System.out.println("Transfer Queue2: "
            + LTQ2);
}

}

Output of TransferQueueExample.java is shown in the snapshot below.

TransferQueueExample.java

Output of TransferQueueExample.java

7. Download the Source Code

That was an example of Java Queue and Priority queue in Java.

Download
You can download the full source code of this example here: Java Queue Example

Last updated on May 03rd, 2021

Photo of Abhinav Nath Gupta

Abhinav holds a Master degree in Computer Science and Engineering from the National Institute of Technology Karnataka. He has finished his graduation from Information Technology Department in the Anand Engineering College, Agra. During his studies he has been involved with a large number of projects ranging from Networking and Cryptography. He works as a software development engineer at a software development firm in bengaluru where he is mainly involved with projects based on Nodejs. He is interested in cryptography, data security, cryptocurrency and cloud computing, and published articles regarding these topics. He can be reached at abhi.aec89@gmail.com.