Re: equals(), Sets, Maps, and degrees of equality
Andreas Leitgeb wrote:
Sean Mitchell wrote:
Anyone ever run into the case where you wish an Object could have
more than one equals(), or that Set and Map implementations would
let you pass in something like a closure to determine key equality?
Yep, I also noticed this lack in the context of "orthogonality".
I just still haven't found any actual practical use for it.
It seems to me that objects can be equal in varying degrees. Let's
consider a class Dog:
public class Dog {
String breed;
String name;
String age;
}
I may want to have a Set<Dog>, which holds only one Dog of each breed,
This is terrible modeling.
In that case, I'd use a Map<String,Dog>, which would be keyed by the breed
name itself, rather than by an arbitrary dog of that breed.
Probably, I'd even create a class Breed, that would encapsulate some more
properties along with the name, and then use the Breed class as key.
+
This is the way to go.
For the example at hand, this would allow filling the structure with breeds
that are "requested" (initially associated with null Dog), then some other
part of the code could pick an appropriate Dog for each requested Breed.
But I may also want a Map<Dog, Owner> in which each Dog is made unique
by name.
That doesn't make sense to me, because the owner would rather be a property
of the dog(*), than of its name. Who's the owner of "any dog called Rufus", btw?
*: natural language is sometimes quite at odds with modelling language. :-)
Andreas is on to something here.
--
Lew