3

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.

2 Answers 2

1

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Why are there so many identical methods?
1

Both the methods internally calls removeFirst(), so there is not difference. ArrayDeque can be used as stack as well as LinkedList hence we have different interfaces, based on how we want to use it (as a stack, queue or linkedlist). It was introduced in version 1.6 of java API which is much later than Stack and LinkedList. As per oracle documentation

ArrayDeque class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue.

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.