简体   繁体   English

Java ArrayDeque 中的 pop() 和 remove() 有本质区别吗?

[英]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. remove 和 pop 都从队列的前面删除并返回一个元素。 They both throw an exception if there's an empty Queue.如果队列为空,它们都会抛出异常。

There is no difference.没有区别。 In fact, pop() and remove() methods both call removeFirst.实际上,pop() 和 remove() 方法都调用 removeFirst。 See https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/ArrayDeque.javahttps://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();
}

Both the methods internally calls removeFirst(), so there is not difference.这两种方法在内部都调用了 removeFirst(),所以没有区别。 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). ArrayDeque 可以用作栈和 LinkedList,因此我们有不同的接口,取决于我们想要如何使用它(作为栈、队列或链表)。 It was introduced in version 1.6 of java API which is much later than Stack and LinkedList.它是在 java API 的 1.6 版本中引入的,比 Stack 和 LinkedList 晚得多。 As per oracle documentation根据 oracle 文档

ArrayDeque class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue. ArrayDeque class 作为栈使用时可能比Stack快,作为队列使用时比LinkedList快。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM