Hi,
Is there a java class that gives the functionality of a Hashtable or a
HashMap but doesn't use the keys' equals() method, but rather a given
equality comparar? Something like IEqualityComparer in c#:
http://msdn2.microsoft.com/en-us/library/ms132151.aspx
I wouldn't want to use a TreeMap with a Comparator as I don't need any
sorting.
Thanks.
Patricia Shanahan - 07 Dec 2007 14:55 GMT
> Hi,
>
[quoted text clipped - 7 lines]
>
> Thanks.
To make HashMap work you would also need to supply a hashCode method
that is consistent with the equality test you are using in the map.
You could use a wrapper class that keeps a reference to the object, and
overrides both equals and hashCode.
Patricia
Meidan - 07 Dec 2007 15:34 GMT
> > Hi,
>
[quoted text clipped - 15 lines]
>
> Patricia
I've thought about it, but code is much clearer when you don't have to
go through a wrapper.
Lew - 07 Dec 2007 15:43 GMT
> I've thought about it, but code is much clearer when you don't have to
> go through a wrapper.
Rrr?

Signature
Lew
Lasse Reichstein Nielsen - 07 Dec 2007 17:52 GMT
> I've thought about it, but code is much clearer when you don't have to
> go through a wrapper.
Then make an adapter class that does it for you:
Map myMap = new EqualityMapAdapter(new HashMap(), myEqualityRelation);
Should be fairly easy to write. Remember that your equality relation
should also provide compatible hash codes, e.g.
public interface Equality {
boolean equals(Object obj1, Object obj2);
int hashCodeOf(Object obj);
}
/L

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Meidan - 09 Dec 2007 09:20 GMT
> > I've thought about it, but code is much clearer when you don't have to
> > go through a wrapper.
[quoted text clipped - 16 lines]
> DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
> 'Faith without judgement merely degrades the spirit divine.'
Thanks. I just figured someone had already written something like that.
Lasse Reichstein Nielsen - 09 Dec 2007 11:45 GMT
> Thanks. I just figured someone had already written something like that.
I'm guessing Commons Collections, but do tell so we can learn :)
/L

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Roedy Green - 12 Dec 2007 16:56 GMT
On Fri, 7 Dec 2007 06:48:16 -0800 (PST), Meidan
<meidan.alon@gmail.com> wrote, quoted or indirectly quoted someone who
said :
>Is there a java class that gives the functionality of a Hashtable or a
>HashMap but doesn't use the keys' equals() method, but rather a given
>equality comparar? Something like IEqualityComparer in c#:
>http://msdn2.microsoft.com/en-us/library/ms132151.aspx
You would subclass that object and provide your custom equals. It
would have a copy constructor i.e. one that ate one of the old class
objects. Then you store these new objects in your HashMap.
Be careful to make sure your hashCode function maintains the contract
with equals.
Your new objects might just POINT to an old object.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com