Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #9831

Re: equals(), Sets, Maps, and degrees of equality

From Daniel Pitts <newsgroup.nospam@virtualinfinity.net>
Newsgroups comp.lang.java.programmer
Subject Re: equals(), Sets, Maps, and degrees of equality
References <24123649.762.1320892382934.JavaMail.geo-discussion-forums@vbmh5> <j9ffe1$3k9$1@dont-email.me> <aa35f1af-81ac-4369-a9f3-5c7c5e161f39@y7g2000vbe.googlegroups.com>
Message-ID <0BVuq.18371$Mg.11636@newsfe13.iad> (permalink)
Date 2011-11-10 11:27 -0800

Show all headers | View raw


On 11/10/11 6:31 AM, Sean Mitchell wrote:
> On Nov 9, 10:11 pm, Eric Sosman<esos...@ieee-dot-org.invalid>  wrote:
>
>>   Seems odd: Why should Fido rather than Rover or Wossname be the
>> sole representative Cocker Spaniel?
>
> Just cuz.
>
> Silly example: Father-in-law is going hunting. He has a kennel full of
> dogs. Wants a hound and a bird dog. Doesn't care which exact ones.
>
>> Two avenues of attack seem plausible.  One, as you mention, is to
>> use a helper class to designate the chosen "identity" attributes.  I
>> think I'd prefer to make it an inner class rather than a wrapper class,
>> but maybe that just means I'm still too hung up on your dogs and breeds.
>
> Hmmm. This is interesting. How would it work? So, I'd have an inner
> class for each type of equality I need, basically exposing its parent
> and providing the desired equals()? Think I'll play around with that
> idea.
>
>
>>       The other approach is to implement your own BreedSet that uses
>> breedEquals() and breedHashCode() instead of the usual methods (and,
>> of course, documents that fact in large red letters).  But this feels
>> an awful lot like the first step down a slippery slope, one that may
>> find you implementing umpty-leven specialized variations of Set and
>> Map and regretting the original choice ...
>
> Yes, as I said in my reply to Owen (which I think I accidentally sent
> as reply to author), if possible I'd rather use smarter people's work
> than write my own implementation. His TreeSet/TrreMap proposal is my
> favourite solution so far.
>
>
> Cheers,
>
> Sean
I've often wanted the equivalent of "Comparator" for the Hash 
implementations of Map/Set.

public interface HashStrategy<T> {
    int hash(T object);
    boolean equivalent(T a, T b);
}


public class DefaultHashStrategy<T> extends HashStrategy<T> {
    public int hash(T object) {
       return object == null ? 0 : object.hashCode();
    }

    public boolean equivalent(T a, T b) {
       return a == null ? b == null : a.equals(b);
    }
}


public class HashMap<K,V> implements Map<K,V> {
     private final HashStrategy<? super K> hashStrategy;

     public HashMap(HashStrategy<? super K> hashStrategy) {
        this.hashStrategy = hashStrategy;
     }
     public HashMap() {
        this(new DefaultHashStrategy<K>());
     }
     // ... the rest of the HashMap implementation.
}

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

equals(), Sets, Maps, and degrees of equality Sean Mitchell <sean@mitchwood.com> - 2011-11-09 18:33 -0800
  Re: equals(), Sets, Maps, and degrees of equality Owen Jacobson <angrybaldguy@gmail.com> - 2011-11-09 22:10 -0500
    Re: equals(), Sets, Maps, and degrees of equality v_borchert@despammed.com (Volker Borchert) - 2011-11-10 04:42 +0000
  Re: equals(), Sets, Maps, and degrees of equality Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-09 22:11 -0500
    Re: equals(), Sets, Maps, and degrees of equality markspace <-@.> - 2011-11-09 22:43 -0800
      Re: equals(), Sets, Maps, and degrees of equality Sean Mitchell <sean@mitchwood.com> - 2011-11-10 06:33 -0800
        Re: equals(), Sets, Maps, and degrees of equality markspace <-@.> - 2011-11-10 07:21 -0800
          Re: equals(), Sets, Maps, and degrees of equality Sean Mitchell <sean@mitchwood.com> - 2011-11-10 07:29 -0800
            Re: equals(), Sets, Maps, and degrees of equality markspace <-@.> - 2011-11-10 10:27 -0800
            Re: equals(), Sets, Maps, and degrees of equality Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-10 20:58 -0500
              Re: equals(), Sets, Maps, and degrees of equality markspace <-@.> - 2011-11-10 19:07 -0800
                Re: equals(), Sets, Maps, and degrees of equality Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-10 23:24 -0500
                Re: equals(), Sets, Maps, and degrees of equality markspace <-@.> - 2011-11-10 20:55 -0800
              Re: equals(), Sets, Maps, and degrees of equality Sean Mitchell <sean@mitchwood.com> - 2011-11-11 10:27 -0800
                Re: equals(), Sets, Maps, and degrees of equality Lew <lewbloch@gmail.com> - 2011-11-11 14:21 -0800
    Re: equals(), Sets, Maps, and degrees of equality Sean Mitchell <sean@mitchwood.com> - 2011-11-10 06:31 -0800
      Re: equals(), Sets, Maps, and degrees of equality Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-11-10 11:27 -0800
  Re: equals(), Sets, Maps, and degrees of equality Roedy Green <see_website@mindprod.com.invalid> - 2011-11-10 16:01 -0800
  Re: equals(), Sets, Maps, and degrees of equality Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-11-11 09:08 +0000
    Re: equals(), Sets, Maps, and degrees of equality Lew <lewbloch@gmail.com> - 2011-11-11 07:27 -0800
      Re: equals(), Sets, Maps, and degrees of equality Sean Mitchell <sean@mitchwood.com> - 2011-11-11 10:28 -0800
        Re: equals(), Sets, Maps, and degrees of equality Lew <lewbloch@gmail.com> - 2011-11-11 14:22 -0800
          Re: equals(), Sets, Maps, and degrees of equality markspace <-@.> - 2011-11-11 15:19 -0800
          Re: equals(), Sets, Maps, and degrees of equality Lew <lewbloch@gmail.com> - 2011-11-13 21:18 -0800
            Re: equals(), Sets, Maps, and degrees of equality Lew <lewbloch@gmail.com> - 2011-11-16 09:15 -0800

csiph-web