HashMap with a custom equality check

P

Patricia Shanahan

Meidan said:
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.

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
 
M

Meidan

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

I've thought about it, but code is much clearer when you don't have to
go through a wrapper.
 
L

Lasse Reichstein Nielsen

Meidan said:
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
 
M

Meidan

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

Thanks. I just figured someone had already written something like that.
 
L

Lasse Reichstein Nielsen

Meidan said:
Thanks. I just figured someone had already written something like that.

I'm guessing Commons Collections, but do tell so we can learn :)

/L
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top