Queue Interface in Java

Queue

In the previous blog, we learned about TreeSet in Java. If you want to know more about it visit TreeSet in Java. In this blog, we will go through Queue Interface. We all are familiar with the word Queue. In general, Queue is nothing but a line of people or vehicles waiting for something. For example, people waiting at the ticket counter or ATM. A similar concept is followed in Queue Interface in Java.


Queue Interface

Queue interface follows the FIFO approach i.e. First In First Out. As the Queue Interface follows the FIFO approach, the elements in the Queue are stored in a sequential manner i.e. in an ordered list where adding or insertion of elements is done at the end and removal or deletion is done at the beginning. As Queue is an interface it required a concrete class for declaration. The most common classes are LinkedList and PriorityQueue.

...

Declaration of Queue Interface

Syntax

public interface Queue extends Collection

Program to demonstrate Queue Interface

import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
  public static void main(String[] args){
    Queue<String> str= new LinkedList<>();
    str.add("Java");
    str.add("React");
    str.add("JS");
    System.out.println("Elements in queue "+ str);
  }
}

Output

Elements in queue [Java, React, JS]

Methods of Queue Interface

  1. add()
  2. add() method is used for adding or inserting the specific element into the queue. If the element is inserted successfully then it returns true, else it throws an exception on failure.

    e.g.

    import java.util.LinkedList;
    import java.util.Queue;
    public class QueueAddExample {
      public static void main(String[] args){
        Queue<String> str= new LinkedList<>();
        str.add("Java");
        str.add("React");
        str.add("JS");
        System.out.println("Elements in queue "+ str);
      }
    }

    Output

    Elements in queue [Java, React, JS]
  3. offer()
  4. offer() method is used for adding or inserting the specific element into the queue. If the element is inserted successfully then it returns true, else returns false

    e.g.

    import java.util.LinkedList;
    import java.util.Queue;
    public class QueueOfferExample {
      public static void main(String[] args){
        Queue<String> str= new LinkedList<>();
        str.offer("Java");
        str.offer("React");
        str.offer("JS");
        System.out.println("Elements in queue "+ str);
      }
    }

    Output

    Elements in queue [Java, React, JS]
  5. element()
  6. element() returns the head of the queue. It throws an exception if the queue is empty.

    e.g.

    import java.util.LinkedList;
    import java.util.Queue;
    public class QueueElementExample {
      public static void main(String[] args){
        Queue<String> str= new LinkedList<>();
        str.offer("Java");
        str.offer("React");
        str.offer("JS");
        System.out.println("Elements in queue: "+ str);
        System.out.println("Head Element: "+ str.element());
      }
    }

    Output

    Elements in queue: [Java, React, JS]
    Head Element: Java
  7. remove()
  8. remove() returns and removes the head of the queue. It throws an exception if the queue is empty.

    e.g.

    import java.util.LinkedList;
    import java.util.Queue;
    public class QueueRemoveExample {
      public static void main(String[] args){
        Queue<String> str= new LinkedList<>();
        str.offer("Java");
        str.offer("React");
        str.offer("JS");
        System.out.println("Elements in queue: "
                + str);
        System.out.println("Element Removed from Queue: "
                + str.remove("React"));
        System.out.println("Updated Elements in Queue: "
                + str);
      }
    }

    Output

    Elements in queue: [Java, React, JS]
    Element Removed from Queue: true
    Updated Elements in Queue: [Java, JS]
  9. peek()
  10. peek() method returns the head of the queue. If the queue is empty it returns null.

    e.g.

    import java.util.LinkedList;
    import java.util.Queue;
    public class QueuePeekExample {
      public static void main(String[] args){
        Queue<Integer> nos= new LinkedList<>();
        nos.add(25);
        nos.add(10);
        nos.add(90);
        System.out.println("Elements in queue: "+ nos);
        int headElement = nos.peek();
        System.out.println("Queue Head: "+ headElement);
      }
    }

    Output

    Elements in queue: [25, 10, 90]
    Queue Head: 25
  11. poll()
  12. poll() method returns and removes the head of the queue. If the queue is empty it returns null.

    e.g.

    import java.util.LinkedList;
    import java.util.Queue;
    public class QueuePollExample {
      public static void main(String[] args){
        Queue<String> str= new LinkedList<>();
        str.offer("Java");
        str.offer("React");
        str.offer("JS");
        System.out.println("Elements in queue: "+ str);
        System.out.println("Method Poll(): "+ str.poll());
        System.out.println("Queue "+ str);
      }
    }

    Output

    Elements in queue: [Java, React, JS]
    Method Poll(): Java
    Queue [React, JS]

    Implementation of LinkedList

    e.g.

    import java.util.Queue;
    import java.util.LinkedList;
    class LinkedListExample {
      public static void main(String[] args) {
        Queue<Integer> nos = new LinkedList<>();
        nos.add(20);
        nos.add(40);
        nos.add(60);
        System.out.println("Queue1: " + nos);
    
        Queue<String> str = new LinkedList<>();
        str.add("Canada");
        str.add("New York");
        str.add("London");
    
        System.out.println("Queue2: " + str);
        int number = nos.peek();
        System.out.println("Accessed Number: " + number);
        System.out.println("Removed Element from Queue2: " + str.poll());
        System.out.println("Updated Queue2: " + str);
      }
    }

    Output

    Queue1: [20, 40, 60]
    Queue2: [Canada, New York, London]
    Accessed Number: 20
    Removed Element from Queue2: Canada
    Updated Queue2: [New York, London]

    Implementation of PriorityQueue

    e.g.

    import java.util.Queue;
    import java.util.PriorityQueue;
    class PriorityQueueExample {
      public static void main(String[] args) {
        Queue<Integer> nos = new PriorityQueue<>();
        nos.add(20);
        nos.add(40);
        nos.add(60);
        System.out.println("Queue1: " + nos);
    
        Queue<String> str = new PriorityQueue<>();
        str.add("Canada");
        str.add("New York");
        str.add("London");
    
        System.out.println("Queue2: " + str);
        int number = nos.peek();
        System.out.println("Accessed Number: " + number);
        System.out.println("Removed Element from Queue2: " + str.poll());
        System.out.println("Updated Queue2: " + str);
      }
    }

    Output

    Queue1: [20, 40, 60]
    Queue2: [Canada, New York, London]
    Accessed Number: 20
    Removed Element from Queue2: Canada
    Updated Queue2: [London, New York]

Core Java Tutorial

Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.