Home » Java programming language

Differences between poll() and remove() methods of Queue Interface in Java

poll() vs remove() methods of Queue Interface in Java: Here, we are going to learn what are the differences between poll() and remove() method of Queue interface in Java programming language?
Submitted by Preeti Jain, on August 05, 2019

poll() vs remove() methods of Queue Interface

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

poll() method

  • 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 (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 (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?


remove() method

  • 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 (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 (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)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.