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
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();
}
1 Comment
Alex78191
Why are there so many identical methods?
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
public E remove() {
return removeFirst();
}
public E pop() {
return removeFirst();
}