Closed GoogleCodeExporter closed 9 years ago
I forgot to mention: AFAIK AbstractBiMap is used for all but the Immutable
Guava implementations, so this affects all maps for which it is relevant
Original comment by bogd...@gmail.com
on 13 Mar 2014 at 5:40
I believe we've historically interpreted the Collections Framework's APIs as
treating .equals() objects as interchangeable in every way, and not making any
guarantees about reference equality. To give another example,
Ints.asList(int[]) does not guarantee that list.get(i) == list.get(i), because
it needs to rebox the Integer on every get() call.
Original comment by lowas...@google.com
on 13 Mar 2014 at 6:16
Guava decided early on that "equals means interchangeable", and we would have
to make rather a lot of changes if if we were to start treating reference
identity as significant. (Think what would happen to HashMultiset! It seems
almost unimplementable.)
We believe that 99% of the time that you care about the distinction between
equal instances, it implies a bug in equals(). Equal means interchangeable. We
realize that the valid need for identity-based structures exist, but in our
experience it's much rarer than users think.
Original comment by kevinb@google.com
on 13 Mar 2014 at 7:46
All that said, it's possible there's a simple and harmless fix here; I don't
know yet. Also there might be doc improvements that could help.
Original comment by kevinb@google.com
on 13 Mar 2014 at 7:47
Yes, well, I can understand the practical argument for generally basing
collections on equals. But my problem is that BiMap extends Map, and yet acts
differently from it without even documenting the incompatibility anywhere.
Note that the Map.put contract says “old value is replaced by the specified
value”, it says nothing about identity, equality, etc. (Technically speaking,
a map as document should perform every step of the put even if the new value is
== to the old.)
Now, “equals means interchangeable” is a defensible decision for the
library — though given the diversity of Equivalences, Comparators, and the
links to memory from weak-reference–based collections, one might hope for a
bit more subtlety — but at the very least it should be mentioned in the
documentation of each class. IMO at least the methods that are declared from
the standard Java collections should document thoroughly every such detail.
Note that HashMultiset only extends Collection and Iterable; the first has a
very 'loose' contract (one shouldn't have many expectations about a
Collection<X> without looking at the details), and the last a very simple one.
Even more, Ints.asList is free to do (and guarantee) whatever it wants, since
it doesn't extend any standard class. The problem here is that BiMap extends
Map but (at least arguably) doesn't follow its contract. Even if you had that
argument and decided that the BiMap behavior is allowed, it should at least be
clearly documented as a difference from most maps.
Original comment by bogd...@gmail.com
on 14 Mar 2014 at 7:46
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:09
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:17
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:07
Original issue reported on code.google.com by
bogd...@gmail.com
on 13 Mar 2014 at 5:37