Java OCPJP7: difference between element(), peek(), poll() and remove() methods of the Queue interface

The Queue interface defines some methods for acting on the first element of the list, which differ in the way they behave, and the result they provide. For the purposes of the OCPJP7 certification exam is important to know how these methods work and to recognize precisely how each of them behaves in some special situations.
These methods are:

  • peek()
  • element()
  • poll()
  • remove()
  • The peek() method retrieves the value of the first element of the queue without removing it from the queue. For each invocation of the 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.
  • 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
  • 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 any exception
  • 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

Let’s look at some examples of how these methods work on a LinkedList, that is an implementation of the Queue interface.
In the first example we add three items to the queue and then we invoke twice the peek() method:

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);
    }
}

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

50
50
[50, 100, 25]

In the second example we create again a LinkedList where we insert the same 3 elements as before and then we invoke twice the poll() method:

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.poll();
        System.out.println(x);

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

        System.out.println(qi);
    }
}

As we can see from the following result, the first poll() extracts and removes the value 50 and the second poll() extracts and removes the value 100. At the end the queue only contains the value 25.

50
100
[25]

In the third example we create a queue in which this time we insert only two elements, and then we invoke three times the poll() method:

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.poll();
        System.out.println(x);

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

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

        System.out.println(qi);
    }
}

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.

50
100
null
[]

In the fourth example we execute the same scenario as above, but replacing the three poll() calls with three remove() calls:

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);
    }
}

As we can see from the following result, the third remove() call throws a NoSuchElementException because it has been executed on an empty queue. This behavior, as we said, is what makes it different from the poll() method.

50
100
Exception in thread "main" java.util.NoSuchElementException
	at java.util.LinkedList.removeFirst(Unknown Source)
	at java.util.LinkedList.remove(Unknown Source)
	at net.davismol.ocpjp7.QueueTest.main(QueueTest.java:20)

The fifth and the sixth examples illustrate the difference in behavior between the peek() method and element() method when invoked on a empty queue:

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

public class QueueTest {

    public static void main(String[] args) {

        Queue<Integer> qi = new LinkedList<>();
        System.out.println(qi.peek());
    }
}

This is the result in this case:

null

Let’s do the same test but this time using the element() method:

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

public class QueueTest {

    public static void main(String[] args) {

        Queue<Integer> qi = new LinkedList<>();
        System.out.println(qi.element());
    }
}

This is the result:

Exception in thread "main" java.util.NoSuchElementException
	at java.util.LinkedList.getFirst(Unknown Source)
	at java.util.LinkedList.element(Unknown Source)
	at net.davismol.ocpjp7.QueueTest.main(QueueTest.java:11)

8 thoughts on “Java OCPJP7: difference between element(), peek(), poll() and remove() methods of the Queue interface

Leave a Reply

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