Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

HashMap with a custom equality check

Thread view: 
Meidan - 07 Dec 2007 14:48 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.