jaggernat
asked on
contains( ) method of ArrayList method
hi experts
I have a method listCounties() which returns a list of countyId fields.
I have a valueObject RegionVO which contains a field regionId.
Now My requirment is if "countyId" field 'contains' regionId or a part of regionId "do somtehing"
i have written something like this
Collection counties= listCounties(); //listCounties returns list of countyId
if (counties.contains(""+regi onVO.getRe gionId()))
{
do something
}
But counties is not getting recognized since its not being typecasted to the CountiesVO value object.
I can do somthing like this to get the individual elements but then i wont be ABLE to use "contains" method of Collection.
for (Iterator countyIt = counties.iterator();
countyIt.hasNext(); ) {
CountiesVO countyVO = (CountiesVO) countyIt.next();
}
any sugessions greatly appreciated
thanks
J
I have a method listCounties() which returns a list of countyId fields.
I have a valueObject RegionVO which contains a field regionId.
Now My requirment is if "countyId" field 'contains' regionId or a part of regionId "do somtehing"
i have written something like this
Collection counties= listCounties(); //listCounties returns list of countyId
if (counties.contains(""+regi
{
do something
}
But counties is not getting recognized since its not being typecasted to the CountiesVO value object.
I can do somthing like this to get the individual elements but then i wont be ABLE to use "contains" method of Collection.
for (Iterator countyIt = counties.iterator();
countyIt.hasNext(); ) {
CountiesVO countyVO = (CountiesVO) countyIt.next();
}
any sugessions greatly appreciated
thanks
J
You need to override equals
Since regionId would be, i guess, unique, you could use that as the basis of the equals implementation for CountiesVO
ASKER
>>>>>You need to override equals
could you please elaborate .
thaNKS FOR prompt response
could you please elaborate .
thaNKS FOR prompt response
ASKER
>>>>Since regionId would be, i guess, unique, you could use that as the basis of the equals implementation for CountiesVO
yes i thought about it. but i am not sure if i can use the "equals implementation"
this is the scenario:
region Id of RegionVO is value "3401"
countyId of CountyVO is value "34017" . so my requirment in that case would be if regionId is a "part of" countyId --> condition is satisfied-->do something.
any help appreciated
yes i thought about it. but i am not sure if i can use the "equals implementation"
this is the scenario:
region Id of RegionVO is value "3401"
countyId of CountyVO is value "34017" . so my requirment in that case would be if regionId is a "part of" countyId --> condition is satisfied-->do something.
any help appreciated
Question first:
>>Now My requirment is if "countyId" field 'contains' regionId
would countyId.equals(regionId) be true?
>>Now My requirment is if "countyId" field 'contains' regionId
would countyId.equals(regionId) be true?
ASKER
would countyId.equals(regionId) be true?
No.
regionId can only be PART OF countyId
thanks
No.
regionId can only be PART OF countyId
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>>>>You'll simply have to iterate explicitly
can you please tell me how to do that???
can you please tell me how to do that???
Well you already posted some iterating code yourself. All you need to do is:
CountiesVO countyVO = (CountiesVO) countyIt.next();
if (countyVO.getId().indexOf( regionVO.g etId()) > -1) {
//matched
}
CountiesVO countyVO = (CountiesVO) countyIt.next();
if (countyVO.getId().indexOf(
//matched
}
ASKER
thanks