Java - Difference Between poll() and remove() Methods of Queue Interface

Java | Queue Interface poll() Vs remove() Methods: In this tutorial, we will learn about the poll() and remove() methods of queue interface in Java and the differences between poll() and remove() methods of Queue Interface. By Preeti Jain Last updated : March 25, 2024

Java poll() Method of Queue Interface

  • This method is available in java.util package.
  • This method is used to retrieve the head element of the Queue or in other words, it is used to retrieve the first element or initial element of the queue.
  • In the case of poll() method, it retrieves the head element of the queue and then removes the head element of the queue.
  • In case of poll() method if the queue is empty then it will return null but it does not throw an exception.
  • The syntax of this method is given below:
            
    public PriorityQueue poll(){
    }
    
  • We don't pass any object as a parameter in the method of the Queue.
  • The return type of this method is not void that means this method return first element of the Queue.

Example of Java poll() Method of Queue Interface (Case 1)

// Java program to demonstrate the behavior of poll() method 
// of Queue in case of if Queue is not empty

import java.util.*;

class QueueClass {
    public static void main(String[] args) {
        // Creating an instance of PriorityQueue class
        PriorityQueue pq = new PriorityQueue();

        // By using add() method to add elements in the Queue
        pq.add(10);
        pq.add(20);
        pq.add(30);
        pq.add(40);
        pq.add(50);

        // Display Current list of the Queue
        System.out.println("Current Queue List:" + pq);

        // By using poll() method of Queue will retrieve 
        // head element with removing head element of the Queue
        System.out.println("The first element of the Queue :" + pq.poll());

        // Display New Queue list of the Queue after implementing poll() method
        System.out.println("New Queue List :" + pq);
    }
}

Output

E:\Programs>javac QueueClass.java

E:\Programs>java QueueClass
Current Queue List:[10, 20, 30, 40, 50]
The first element of the Queue :10
New Queue List :[20, 40, 30, 50]

Example of Java poll() Method of Queue Interface (Case 2)

// Java program to demonstrate the behavior of poll() method 
// of Queue in case of if Queue is empty

import java.util.*;

class QueueClass {
    public static void main(String[] args) {
        // Creating an instance of PriorityQueue class
        PriorityQueue pq = new PriorityQueue();

        // Display Current list of the Queue
        System.out.println("Current Queue List:" + pq);

        // By using poll() method of Queue will return null if queue is empty
        System.out.println("The result of Queue :" + pq.poll());

        // Display New Queue list of the Queue after implementing poll() method
        System.out.println("New Queue List :" + pq);
    }
}

Output

E:\Programs>javac QueueClass.java

E:\Programs>java QueueClass
Current Queue List:[]
The first element of the Queue :null
New Queue List :[]

Now, we will see how remove() method differs from poll() method of Queue interface?

Java remove() Method of Queue Interface

  • This method is available in java.util package.
  • This method is used to remove the head element of the Queue and retrieve the first element of the Queue like poll() method.
  • In the case of remove() method, it retrieves the head element and removes the first element of the Queue as well by calling remove() method.
  • In case of remove() method, if Queue is empty then, in that case, it throws an exception NoSuchElementFoundException but it does not return null like of poll() method.
  • We don't pass any object as a parameter in the method of the Queue.
  • The syntax of the method is given below:
        public boolean remove(){}
    
  • The return type of this method is not void that means the return type of this method is boolean so it returns true after removing the element else return false.

Example of Java remove() Method of Queue Interface (Case 1)

// Java program to demonstrate the behavior of remove() method 
// of Queue in case of if Queue is not empty

import java.util.*;

class QueueClass {
    public static void main(String[] args) {
        // Creating an instance of PriorityQueue class
        PriorityQueue pq = new PriorityQueue();

        // By using add() method to add elements in the Queue
        pq.add(10);
        pq.add(20);
        pq.add(30);
        pq.add(40);
        pq.add(50);

        // Display Current list of the Queue
        System.out.println("Current Queue List:" + pq);

        // By using remove() method of Queue will retrieve 
        // head element with removing head element of the Queue
        System.out.println("The first element of the Queue :" + pq.remove());

        // Display New Queue list of the Queue after 
        // implementing remove() method
        System.out.println("New Queue List :" + pq);
    }
}

Output

E:\Programs>javac QueueClass.java

E:\Programs>java QueueClass
Current Queue List:[10, 20, 30, 40, 50]
The first element of the Queue :10
New Queue List :[20, 40, 30, 50]

Example of Java remove() Method of Queue Interface (Case 2)

// Java program to demonstrate the behavior of remove() method 
// of Queue in case of if Queue is empty

import java.util.*;

class QueueClass {
    public static void main(String[] args) {
        // Creating an instance of PriorityQueue class
        PriorityQueue pq = new PriorityQueue();

        // Display Current list of the Queue
        System.out.println("Current Queue List:" + pq);

        // By using remove() method of Queue will throw 
        // an exception if queue is empty
        System.out.println("The result of Queue :" + pq.remove());

        // Display New Queue list of the Queue after 
        // implementing remove() method
        System.out.println("New Queue List :" + pq);
    }
}

Output

E:\Programs>javac QueueClass.java

E:\Programs>java QueueClass
Current Queue List:[]
Exception in thread "main" java.util.NoSuchElementException
        at java.util.AbstractQueue.remove(AbstractQueue.java:117)
        at QueueClass.main(QueueClass.java:20)

Difference Between poll() and remove() Methods of Queue Interface in Java

The below table contains the difference between poll() and remove() methods of Queue Interface in Java:

Feature poll() Method remove() Method
Behavior on Empty Queue The poll() method returns null if the queue is empty. The remove() method throws NoSuchElementException if the queue is empty.
Return Type The poll() method returns the head of the queue, or null if empty. The remove() method returns the head of the queue.
Exception Thrown The poll() method does not throw an exception. The remove() method throws NoSuchElementException if the queue is empty.
Usage The poll() method commonly used for dequeuing elements. The remove() method commonly used for dequeuing elements.
Error Handling The poll() method requires explicit null-check for empty queue. The remove() method handles empty queue scenario through exception.
State of Queue The poll() method does not modify the state of the queue. The remove() method modifies the state of the queue by removing an element.
Performance The poll() method generally faster as it does not throw exceptions. The remove() method is slower due to exception handling overhead.
Idempotent The poll() method idempotents operation (multiple calls yield the same result). The remove() method idempotents operation.
Interface Implementation The poll() method is a required method for Queue interface. The remove() method is an optional method for Queue interface.
Presence of Element The poll() method doesn't rely on the presence of an element. The remove() method relies on the presence of an element.


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.