为什么类 ArrayDeque 定义为
public class ArrayDeque<E> extends AbstractCollection<E>
implements Deque<E>, Cloneable, Serializable
不像
public class ArrayDeque<E> extends AbstractQueue<E>
implements Deque<E>, Cloneable, Serializable
如果您查看下图(其中有错误,ArrayDeque 与 Set 的关系是完全错误的),ArrayDeque 继承自 AbstractQueue 是有道理的,因为它通过 Deque 接口(interface)间接实现了 Queue 接口(interface)。
(来源:academic.ru)
最佳答案
在 OpenJDK core-libs 上发布的 jsr166 维护者之一:
I don't think of ArrayDeque as a queue/deque at all. We just have two fundamental data structures here (ArrayList (ordinary resizable array) and ArrayDeque (circular resizable array)) both of which can implement List and Queue in reasonable ways, and ArrayDeque can additionally implement Deque.
扩展 AbstractCollection 和实现 Deque 与该观点一致。
这也是一个保守的举动,因为 it would allow future versions of ArrayDeque to extend AbstractList实现大部分 List 接口(interface)。只有当它被认为是合法的和/或 desirable to implement both List and Queue 时才会发生这种情况。由于冲突的 equals/hashCode 契约(Contract)。
关于java - 为什么 ArrayDeque 类不从 AbstractQueue 扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25156772/