我知道 Hashtable 不允许空值,因为它是遗留的,由 HashMap 修复。树中不允许为空,因为排序会成为一个问题。但是为什么在 ArrayDeque 中不允许使用 null?
最佳答案
来自 Deque
的 Javadoc:
While
Deque
implementations are not strictly required to prohibit the insertion of null elements, they are strongly encouraged to do so. Users of anyDeque
implementations that do allow null elements are strongly encouraged not to take advantage of the ability to insert nulls. This is so becausenull
is used as a special return value by various methods to indicated that the deque is empty.
ArrayDeque
实现还使用 null
来指示空元素(如 sestus' 和 Aman jangra 的回答),所以推理有两层:契约(Contract) 和实现细节。
值得注意的是,ArrayDeque
可以轻松支持 null 元素,而无需太多额外的复杂性,因此更有说服力的原因似乎是 Deque< 的建议
接口(interface)。
关于java - 为什么 ArrayDeque 中不允许空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34851512/