Would this be a good idea? How can it be solved, if not, nowadays?
I think it would be interesting adding an
final boolean identical(Obj obj){
return (this==obj);
}
so we had an improved equals (logical equals)
boolean equals (Obj obj){
return identical(obj); // by default, but its overrideable
}
This question arised from the need in this other question (A Mechanism for having different equals (physical equals and logical equals) on objects in Collection) of having a way to compare a list of identical pointers to a list of equally objects. With that idea we could add to the Collection interface:
coll.equals(coll2)
coll.identical(coll2)
coll.identicalElem(coll2){
//current equals implementation of collections but calling identical to compare objects
}
What do you think?
The default equals()
implementation you posted is already the one in java.lang.Object
. There's no real point in overriding it with the same implementation.
Regarding your 3 methods to add to the collection interface:
coll.equals(coll2)
: this one is already in the Collection interface.
coll.identical(coll2)
: equivalent to coll == coll2
, but less readable. I don't see the point of such a method.
coll.identicalElem(coll2)
: this method indeed doesn't exist, but I've never had the need for such a method, so I think it should not clutter the API of Collection. You could use Guava's Equivalence to do that:
Equivalence.identity().pairWise().equivalent(coll1, coll2);