• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

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
0
jaggernat
Asked:
jaggernat
  • 5
  • 5
1 Solution
 
CEHJCommented:
You need to override equals
0
 
CEHJCommented:
Since regionId would be, i guess, unique, you could use that as the basis of the equals implementation for CountiesVO
0
 
jaggernatAuthor Commented:
>>>>>You need to override equals

could you please elaborate .

thaNKS FOR prompt response
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jaggernatAuthor Commented:
>>>>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
0
 
CEHJCommented:
Question first:

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

would countyId.equals(regionId) be true?
0
 
jaggernatAuthor Commented:
would countyId.equals(regionId) be true?

No.
regionId can only be PART OF countyId

thanks
0
 
CEHJCommented:
Ah we cross-posted. You're right. equals is not appropriate for 'fuzzy matching'. You'll simply have to iterate explicitly
0
 
jaggernatAuthor Commented:
>>>>You'll simply have to iterate explicitly

can you please tell me how to do that???

0
 
CEHJCommented:
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
}
0
 
jaggernatAuthor Commented:
thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now