Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!novia!news-out.readnews.com!news-xxxfer.readnews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: equals(), Sets, Maps, and degrees of equality Date: Fri, 11 Nov 2011 07:27:21 -0800 (PST) Organization: http://groups.google.com Lines: 46 Message-ID: <4407931.1762.1321025241857.JavaMail.geo-discussion-forums@prep8> References: <24123649.762.1320892382934.JavaMail.geo-discussion-forums@vbmh5> Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 173.164.137.214 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1321025242 30947 127.0.0.1 (11 Nov 2011 15:27:22 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 11 Nov 2011 15:27:22 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9847 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, which holds only one Dog of each breed, This is terrible modeling. > In that case, I'd use a Map, 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 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