I know Hashtable doesn't allow null values as it was legacy, which was fixed by HashMap. Null is not allowed in a tree as sorting would be an issue. But why us null not allowed in ArrayDeque?
Null elements are prohibited in the ArrayDeque. ArrayDeque class is likely to be faster than Stack when used as a stack.
Null elements are prohibited. This class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue. Most ArrayDeque operations run in amortized constant time. Exceptions include remove , removeFirstOccurrence , removeLastOccurrence , contains , iterator.
Queue implementations generally do not allow insertion of null elements, although some implementations, such as LinkedList , do not prohibit insertion of null.
Hashtable does not allow any null as key or value, and Hashtable is legacy and only single thread can access at a time.
From the Javadoc for Deque
:
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.
The ArrayDeque
implementation also uses null
to indicate an empty element (as explained in sestus' and Aman jangra's answers), so the reasoning is two-fold: contract and implementation details.
It's worth noting that it ArrayDeque
could easily support null elements without much additional complexity, so the more compelling reason seems to be the suggestion of the Deque
interface.
This may be so because null is used as a special return value by various methods to indicate that the deque is empty.However not all deques prohibit insertion of null values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With