Saturday, 8 June 2019

Core Java | Difference between element(), peek(), poll() and remove() methods of the Queue interface

The peek() method retrieves the value of the first element of the queue without removing it from the queue. For each invocation of the peek() method, we always get the same value and its execution does not affect the size of the queue. If the queue is empty the peek() method returns null.

import java.util.LinkedList;
import java.util.Queue;

public class QueueTest {
       public static void main(String[] args) {
              Queue<Integer> qi = new LinkedList<>();
              qi.add(50);
              qi.add(100);
              qi.add(25);

              Integer x = qi.peek();
              System.out.println(x);

              x = qi.peek();
              System.out.println(x);
              System.out.println(qi);
       }
}
Output:
50
50
[50, 100, 25]

As we can see from the above result, peek always returns the first value and in the end, the queue still contains all the three elements inserted.

The element() method behaves like peek(), so it again retrieves the value of the first element without removing it. Unlike peek(), however, if the list is empty element() throws a NoSuchElementException.

import java.util.LinkedList;
import java.util.Queue;

public class QueueTest {
       public static void main(String[] args) {
              Queue<Integerqi = new LinkedList<>();

              // Queue is empty, will throw NoSuchElementException
              Integer x = qi.element();
              System.out.println(x);
       }
}
Output:
Exception in thread "main" java.util.NoSuchElementException
       at java.util.LinkedList.getFirst(LinkedList.java:244)
       at java.util.LinkedList.element(LinkedList.java:663)
       at com.algorithmforum.collection.QueueTest.main(QueueTest.java:12)

The poll() method retrieves the value of the first element of the queue by removing it from the queue. At each invocation, it removes the first element of the list and if the list is already empty it returns null but does not throw an exception.

import java.util.LinkedList;
import java.util.Queue;

public class QueueTest {
       public static void main(String[] args) {
              Queue<Integerqi = new LinkedList<>();
              qi.add(50);
              qi.add(100);

              Integer x = qi.poll();
              System.out.println(x);

              x = qi.poll();
              System.out.println(x);

              x = qi.poll();
              System.out.println(x);
              System.out.println(qi);
       }
}
Output:
50
100
null
[]

As we can see from the following result, the first two invocations of poll () remove the two elements of the list that is now empty. The third poll() call returns null, without throwing no exception and the list at the end does not contain any element.

The remove() method behaves as the poll() method, so it removes the first element of the list and if the list is empty it throws a NoSuchElementException.

import java.util.LinkedList;
import java.util.Queue;

public class QueueTest {
       public static void main(String[] args) {
              Queue<Integer> qi = new LinkedList<>();
              qi.add(50);
              qi.add(100);

              Integer x = qi.remove();
              System.out.println(x);

              x = qi.remove();
              System.out.println(x);

              x = qi.remove();
              System.out.println(x);
              System.out.println(qi);
       }
}
Output:
50
100
Exception in thread "main"
java.util.NoSuchElementException
       at java.util.LinkedList.removeFirst(LinkedList.java:270)
       at java.util.LinkedList.remove(LinkedList.java:685)
       at com.algorithmforum.collection.QueueTest.main(QueueTest.java:20)



1 comment:

  1. The NoSuchElementException in Java is thrown when one tries to access an iterable beyond its maximum limit. This means that, this exception is thrown by various accessor methods to indicate that the element being requested does not exist. The next() method in Java returns the next element in the iteration or NoSuchElementException if the iteration has no more elements. The solution to this exception is to check whether the next position of an iterable is filled or empty . The following methods are used to check the next position:

    hasNext()
    hasMoreElements()

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...