[concurrency-interest] LinkedBlockingQueue does not throw NullPointerException for the method call contains

Doug Lea dl at cs.oswego.edu
Sun Apr 15 17:15:44 EDT 2007

Szabolcs Ferenczi wrote:
> It is not inserted but queried, see "q.contains(null)"
> The documentation for method "public boolean contains(Object o)" says:
> "Throws:
> ...
> NullPointerException - if the specified element is null and this
> collection does not permit null elements (optional)"

I believe the optionality refers to throwing the exception. In collections
(like Hashtable etc) where nulls cannot be handled at all (you cannot
get the hashCode of null), methods contains, remove, and so on
throw exceptions for null arguments. In others, they simply return
failure indications.

I suppose this could be made more consistent than it is, but
given the preference some people have to allow nulls as elements
in some kinds of collections, but to have others where they cannot
possibly be present, you cannot make it entirely consistent.


More information about the Concurrency-interest mailing list