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