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 / October 2006

Tip: Looking for answers? Try searching our database.

Two equals method for Class and Collection.contains?

Thread view: 
GeorgeH - 14 Oct 2006 11:12 GMT
Hello!

I have a following situation:

A class with members

public class Item
{
 Integer id;
 String  value:
...
}

I store instances of this Item class into ArrayList.

Item class has also a equals method for Collection.contains method.
In equals method I check that instance type is ok and that values
of id and value are same than reference object's .

BUT, sometimes I only need equals method that would
check only if id member variable is same, not value member.

I can't write two equals methods to Item class so, what could
be a practical way to check if ArrayList has Item class instance that has
same id that I'm searching, but value member would then be ignored?
I try avoid loops and then checking each Item objects id member.

But how to implement this, any tips?

Cheers!
Pradyumna.Jena@gmail.com - 14 Oct 2006 12:35 GMT
hi
we need to define the notion of equalness according to our
requirement.and it should satisfy
the contract specified by that method.and its very easy to get it
wrong.

if you really need to kinds of behaviour from ur equals
method(interpretation of value field)
i think we can define some sort boolean flag as class varible that will
decide whether we want to consider value in comparision or not...

> Hello!
>
[quoted text clipped - 26 lines]
>
> Cheers!
GeorgeH - 14 Oct 2006 15:55 GMT
Yes I will try that kind of approach, thanks!

> hi
> we need to define the notion of equalness according to our
[quoted text clipped - 37 lines]
>>
>> Cheers!
Patricia Shanahan - 14 Oct 2006 16:21 GMT
> hi
> we need to define the notion of equalness according to our
[quoted text clipped - 6 lines]
> i think we can define some sort boolean flag as class varible that will
> decide whether we want to consider value in comparision or not...

That seems a very risky suggestion. Suppose the main thread is searching
the collection for a matching id, and while that is happening the event
handling thread needs to check whether two Item objects really are equal.

If the id-only form of equality is the correct one for all the
collection actions, I would suggest thinking in terms of putting the Id
in the collection, not the Item:

public class Item
{
  Id id;
  String value;
...
  private class Id
  {
     int idNum;
...
     public boolean equals(Object o)
     {
       // Equal if o not null, same class, and idNum matches
...
     }
     private Item getItem()
     {
       return Item.this;
     }
  }
}

Two Item.Id objects are equal if, and only if, they represent the same
id. Two Item objects are equal if, and only if, they have the same id
and the same value.

If the id-only check is only used on some searches, and the collection
can contain two items with the same id but different values, just write
the loop yourself. You can always tuck it away in a method, so it does
not clutter up the code that calls it, and it makes very little
difference whether the ArrayList is searched by a loop in your code or
one in the ArrayList code.

Patricia


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.