Link to home
Start Free TrialLog in
Avatar of jaggernat
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(""+regionVO.getRegionId()))
                   {

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of jaggernat
jaggernat

ASKER

>>>>>You need to override equals

could you please elaborate .

thaNKS FOR prompt response
>>>>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
Question first:

>>Now My requirment is if "countyId" field  'contains' regionId

would countyId.equals(regionId) be true?
would countyId.equals(regionId) be true?

No.
regionId can only be PART OF countyId

thanks
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>>>You'll simply have to iterate explicitly

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.getId()) > -1) {
    //matched
}
thanks