S
Sean Mitchell
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?
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, irrespective of name of age. In this case my equals()/hashcode() would only consider breed.
But I may also want a Mag<Dog, Owner> in which each Dog is made unique by name.
And of course, there is the most intuitive case where I want to use equals() to see if the instances map on all three fields.
I suppose I could create a wrapper class for each purpose which only overrides equals() and hashcode(), but that seems very unsatisfying and inefficient.
I'm interested in how other people have dealt with this. Surprisingly, I have not been able to Google up much on this subject.
Thoughts?
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, irrespective of name of age. In this case my equals()/hashcode() would only consider breed.
But I may also want a Mag<Dog, Owner> in which each Dog is made unique by name.
And of course, there is the most intuitive case where I want to use equals() to see if the instances map on all three fields.
I suppose I could create a wrapper class for each purpose which only overrides equals() and hashcode(), but that seems very unsatisfying and inefficient.
I'm interested in how other people have dealt with this. Surprisingly, I have not been able to Google up much on this subject.
Thoughts?