Two equals method for Class and Collection.contains?

G

GeorgeH

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!
 
P

Pradyumna.Jena

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...
 
G

GeorgeH

Yes I will try that kind of approach, thanks!

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...
 
P

Patricia Shanahan

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...

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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top