Closed GoogleCodeExporter closed 9 years ago
I tend to side with the notion that contains(null) should return false, but
others believe (sometimes strongly) that for a collection that doesn't allow
nulls, returning false would just mask a probable bug. So we never embarked on
making all our collections consistent because we couldn't agree which direction
to go. We also figured at least the JDK is just as inconsistent as we are on
this, if not more so.
I find the inconsistency ... tolerable.
Then the same problem exists with ClassCastExceptions (typically only in
SortedSets and SortedMaps, since Comparator.compare() and
Comparable.compareTo() are spec'd to throw that, unlike Object.equals(). I
have sometimes used the terms "Type A" and "Type B" collections, like the
well-known personality types. A Type B collection is chill, telling you "no, I
don't have that," while a Type A collection is like "HOW DARE YOU EVEN ASK ME
THAT!"
(BTW, contrary to your opening statement, our general-purpose collections like
HashBiMap, ArrayListMultimap and so forth do permit null. Immutable and
concurrent ones don't. This is reasonably consistent with the JDK.)
Original comment by kevinb@google.com
on 13 Jul 2010 at 8:59
Regarding contains(null): I agree that it should return false. I think the
notion of throwing NPE is too anal. The contract for Object.equals() specifies
that false should be returned if null is passed in, and I think a proper
abstraction of Collection.contains(o) is: return element1.equals(o) ||
element2.equals(o) || ...
However, I would love to see the Immutable collections support null.
Original comment by ray.j.gr...@gmail.com
on 11 Aug 2010 at 11:20
Original comment by fry@google.com
on 28 Jul 2011 at 5:21
We actually added tests for this a month or two ago and found that everything
already passes.
Original comment by kevinb@google.com
on 5 Oct 2011 at 5:10
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:15
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:09
Original issue reported on code.google.com by
fin...@gmail.com
on 13 Jul 2010 at 11:56