Queue in Java

Java Queue interface belongs to the Collection Framework, and it orders the elements in FIFO(First In First Out) manner. The element first added to the Queue will be the first to come out of it.

Implementations of the Queue interface are LinkedList and PriorityQueue classes.

LinkedList

LinkedList is also the implementation of the List interface, and here each element contains a pointer to the next element, and elements are linked to each other.

PriorityQueue

PriorityQueue class provides the facility of using queues. It is used when the objects need to be processed based on priority. The elements of the PriorityQueue are ordered according to their natural ordering.

How to create a Queue in Java?

The Queue is an interface, so we cannot instantiate it. We create an instance of LinkedList or PriorityQueue and assign it to the Queue.
Example:

 Queue queue1 = new LinkedList();
 Queue queue2 = new PriorityQueue();


How to add elements to a Queue in Java?

We can add elements to the Queue using the offer(Object o) method.
Example:

class Test {
  
  public static void main(String[] args) {
    Queue<String> queue = new LinkedList();
    queue.offer("Steve");
    queue.offer("Megan");
    queue.offer("Ryan");
    queue.offer("Melissa");

    System.out.println(queue);
  }
}
Output: [Steve, Megan, Ryan, Melissa]
 
We can also use the add(Object o) method to add elements to the Queue. In this example, we will use the PriorityQueue class:
class Test {

  public static void main(String[] args) {
    Queue<String> queue = new PriorityQueue<>();
    queue.add("Steve");
    queue.add("Megan");
    queue.add("Ryan");
    queue.add("Melissa");

    System.out.println(queue);
  }
}
Output: [Megan, Melissa, Ryan, Steve]

How to remove elements from the Queue in Java?

We can remove queue elements in four ways:

By element

We can use the remove(Object o) method and specify the exact element we want to remove from the queue.
Example:

class Test {

  public static void main(String[] args) {
    Queue<String> queue = new LinkedList<>();
    queue.offer("Steve");
    queue.offer("Megan");
    queue.offer("Ryan");
    queue.offer("Melissa");

    queue.remove("Melissa");

    System.out.println(queue);
  }
}
Output: [Steve, Megan, Ryan]

Remove the first element from the Queue

We can use the remove() method to remove the first element.
The element which we first added will be removed.
Example:

class Test {

  public static void main(String[] args) {
    Queue<String> queue = new LinkedList<>();
    queue.offer("Steve");
    queue.offer("Megan");
    queue.offer("Ryan");
    queue.offer("Melissa");

    queue.remove();

    System.out.println(queue);
  }
}
Output: [Megan, Ryan, Melissa]

Removing all elements that are present in the specified collection

We can use removeAll(Collection<?> c) to remove all elements present in the collection we passed in as a parameter.
Example:

class Test {

  public static void main(String[] args) {
    Queue<String> queue = new PriorityQueue<>();
    queue.offer("Steve");
    queue.offer("Megan");

    Queue<String> queue2 = new PriorityQueue<>();
    queue2.offer("Steve");
    queue2.offer("Megan");
    queue2.offer("Ryan");
    queue2.offer("Melissa");

    queue2.removeAll(queue);

    System.out.println(queue2);
  }
}
Output: [Melissa, Ryan]

Clear the queue / remove all elements from the queue

We can use the clear() method to remove all elements from the queue.
Example:

class Test {

  public static void main(String[] args) {
    Queue<String> queue = new PriorityQueue<>();
    queue.offer("Steve");
    queue.offer("Megan");

    queue.clear();

    System.out.println(queue);
  }
}
Output: []
 

How to get the size of the Queue in Java?

To find out the size of the Queue in Java, we use the size() method.

Example:

class Test {

  public static void main(String[] args) {
    Queue<String> queue = new PriorityQueue<>();
    queue.offer("Steve");
    queue.offer("Megan");
    queue.offer("Ryan");
    queue.offer("Melissa");

    System.out.println("The size of the queue: " + queue.size());
  }
}
Output: The size of the queue: 4

Some more useful methods of the Queue interface:

element()

Retrieves, but does not remove, the head of this queue.

class Test {

  public static void main(String[] args) {
    Queue<String> queue = new LinkedList<>();
    queue.offer("Steve");
    queue.offer("Megan");
    queue.offer("Ryan");
    queue.offer("Melissa");

    System.out.println(queue.element());
  }
}
Output: Steve
 

poll()

Retrieves and removes the head of this queue or returns null if this queue is empty.

class Test {

  public static void main(String[] args) {
    Queue<String> queue = new LinkedList<>();
    queue.offer("Steve");
    queue.offer("Megan");
    queue.offer("Ryan");
    queue.offer("Melissa");

    System.out.println(queue.poll());

    System.out.println(queue);
  }
}
Output: Steve [Megan, Ryan, Melissa]


peek()

Retrieves, but does not remove, the head of this queue or returns null if this queue is empty.

class Test {

  public static void main(String[] args) {
    Queue<String> queue = new LinkedList<>();
    queue.offer("Steve");
    queue.offer("Megan");
    queue.offer("Ryan");
    queue.offer("Melissa");

    System.out.println(queue.peek());

    System.out.println(queue);
  }
}
Output: Steve [Steve, Megan, Ryan, Melissa]
 
That’s it!

Leave a Reply

Your email address will not be published. Required fields are marked *