Search code examples
javaqueuearraydeque

Is there a material difference between pop() and remove() in Java ArrayDeque?


Both remove and pop remove and return an element from the front of the Queue. They both throw an exception if there's an empty Queue.


Solution

  • There is no difference. In fact, pop() and remove() methods both call removeFirst. See https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/ArrayDeque.java

    public E remove() {
        return removeFirst();
    }
    
    public E pop() {
        return removeFirst();
    }