Muthu Muthu - 1 year ago 111
Java Question

Why null values are not allowed in ArrayDeque?

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?

Answer Source

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 any Deque implementations that do allow null elements are strongly encouraged not to take advantage of the ability to insert nulls. This is so because null 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download